Skip to content

Commit a8d4788

Browse files
committed
feat(server): Add JSON serialization utility and improve debug logging
1 parent b3abf88 commit a8d4788

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

src/main/java/com/github/codeboyzhou/mcp/declarative/server/McpSyncServerPromptRegister.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.codeboyzhou.mcp.declarative.annotation.McpPrompt;
44
import com.github.codeboyzhou.mcp.declarative.annotation.McpPromptParam;
5+
import com.github.codeboyzhou.mcp.declarative.util.JsonHelper;
56
import com.github.codeboyzhou.mcp.declarative.util.ReflectionHelper;
67
import io.modelcontextprotocol.server.McpServerFeatures;
78
import io.modelcontextprotocol.server.McpSyncServer;
@@ -44,6 +45,7 @@ public McpServerFeatures.SyncPromptSpecification createComponentFrom(Class<?> cl
4445
final String description = promptMethod.description();
4546
List<McpSchema.PromptArgument> promptArguments = createPromptArguments(method);
4647
McpSchema.Prompt prompt = new McpSchema.Prompt(name, description, promptArguments);
48+
logger.debug("Registering prompt: {}", JsonHelper.toJson(prompt));
4749
return new McpServerFeatures.SyncPromptSpecification(prompt, (exchange, request) -> {
4850
Object result;
4951
try {

src/main/java/com/github/codeboyzhou/mcp/declarative/server/McpSyncServerResourceRegister.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.codeboyzhou.mcp.declarative.server;
22

33
import com.github.codeboyzhou.mcp.declarative.annotation.McpResource;
4+
import com.github.codeboyzhou.mcp.declarative.util.JsonHelper;
45
import com.github.codeboyzhou.mcp.declarative.util.ReflectionHelper;
56
import io.modelcontextprotocol.server.McpServerFeatures;
67
import io.modelcontextprotocol.server.McpSyncServer;
@@ -42,6 +43,7 @@ public McpServerFeatures.SyncResourceSpecification createComponentFrom(Class<?>
4243
res.uri(), name, res.description(), res.mimeType(),
4344
new McpSchema.Annotations(List.of(res.roles()), res.priority())
4445
);
46+
logger.debug("Registering resource: {}", JsonHelper.toJson(resource));
4547
return new McpServerFeatures.SyncResourceSpecification(resource, (exchange, request) -> {
4648
Object result;
4749
try {

src/main/java/com/github/codeboyzhou/mcp/declarative/server/McpSyncServerToolRegister.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.github.codeboyzhou.mcp.declarative.annotation.McpJsonSchemaDefinitionProperty;
55
import com.github.codeboyzhou.mcp.declarative.annotation.McpTool;
66
import com.github.codeboyzhou.mcp.declarative.annotation.McpToolParam;
7+
import com.github.codeboyzhou.mcp.declarative.util.JsonHelper;
78
import com.github.codeboyzhou.mcp.declarative.util.ReflectionHelper;
89
import io.modelcontextprotocol.server.McpServerFeatures;
910
import io.modelcontextprotocol.server.McpSyncServer;
@@ -49,6 +50,7 @@ public McpServerFeatures.SyncToolSpecification createComponentFrom(Class<?> claz
4950
McpSchema.JsonSchema paramSchema = createJsonSchema(method);
5051
final String name = toolMethod.name().isBlank() ? method.getName() : toolMethod.name();
5152
McpSchema.Tool tool = new McpSchema.Tool(name, toolMethod.description(), paramSchema);
53+
logger.debug("Registering tool: {}", JsonHelper.toJson(tool));
5254
return new McpServerFeatures.SyncToolSpecification(tool, (exchange, params) -> {
5355
Object result;
5456
boolean isError = false;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.codeboyzhou.mcp.declarative.util;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.github.codeboyzhou.mcp.declarative.exception.McpServerException;
6+
7+
public final class JsonHelper {
8+
9+
private static final ObjectMapper MAPPER = new ObjectMapper();
10+
11+
public static String toJson(Object object) {
12+
try {
13+
return MAPPER.writeValueAsString(object);
14+
} catch (JsonProcessingException e) {
15+
throw new McpServerException("Error converting object to JSON", e);
16+
}
17+
}
18+
19+
}

0 commit comments

Comments
 (0)