feat(mcp): Add native MCP server implementation with example and docs#1432
feat(mcp): Add native MCP server implementation with example and docs#1432BEASTSHRIRAM wants to merge 3 commits into
Conversation
|
AgentScope Developer seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
There was a problem hiding this comment.
Pull request overview
Introduces a native, in-house MCP (Model Context Protocol) server implementation in agentscope-core (JSON-RPC 2.0 messages, handler/router, ToolManager, StdioTransport, TcpTransport, McpServer facade) plus a calculator example module exposing tools both as an MCP server and as ReActAgent tools, and a 500‑line user guide. The intent is to let AgentScope Java apps build and expose MCP servers (previously only client integration existed). Several pieces of the new server stack have correctness, transport, naming and convention issues that should be resolved before merge.
Changes:
- Add
io.agentscope.core.mcp.{message,handler,schema,tool,server,transport}packages implementing JSON-RPC 2.0 plumbing,initialize/tools/list/tools/callhandlers, aToolManager, andStdioTransport/TcpTransport. - Add
agentscope-examples/mcp-native-example(Calculator/OpenAI tools,McpServerRunner,ReActAgentCliRunner, fat-JAR shade build) and wire it into the examples reactor. - Add
docs/en/task/mcp-native.mduser guide and ajackson-datatype-jdk8dependency toagentscope-core.
Reviewed changes
Copilot reviewed 48 out of 48 changed files in this pull request and generated 14 comments.
Show a summary per file
| File | Description |
|---|---|
| agentscope-core/pom.xml | Adds jackson-datatype-jdk8 for Optional serialization used by new MCP schema records. |
| agentscope-core/src/main/java/io/agentscope/core/mcp/message/*.java | Base JsonRpcMessage + Request/Response/Notification/Error types for JSON-RPC 2.0. |
| agentscope-core/src/main/java/io/agentscope/core/mcp/handler/*.java | MethodHandler SPI, registry, router, initialize/tools/list/tools/call handlers; uses java.util.logging instead of SLF4J. |
| agentscope-core/src/main/java/io/agentscope/core/mcp/schema/*.java | Auto-generated MCP schema records with PascalCase builder methods and several empty placeholder types (RequestId, ContentBlock). |
| agentscope-core/src/main/java/io/agentscope/core/mcp/tool/{Tool,ToolManager}.java | Server-side tool SPI and concurrent registry; name collides with schema.Tool. |
| agentscope-core/src/main/java/io/agentscope/core/mcp/server/McpServer.java | Facade wiring transport, handlers, tool manager; runs router on a single-thread executor. |
| agentscope-core/src/main/java/io/agentscope/core/mcp/transport/{Transport,TransportException,StdioTransport,TcpTransport}.java | Transport SPI plus stdio and TCP implementations; TcpTransport is client-only despite being documented as server-side, and the private startReadThread() in both is never invoked. |
| agentscope-core/src/test/java/io/agentscope/core/mcp/**/*.java | New unit/Mockito tests for messages, handler registry, router, tool manager and CallToolHandler. |
| agentscope-examples/pom.xml | Registers the new mcp-native-example module. |
| agentscope-examples/mcp-native-example/pom.xml | Shaded fat-JAR build with McpServerRunner as main class. |
| agentscope-examples/mcp-native-example/src/main/java/.../CalculatorTool.java | Sample MCP tool returning a result map. |
| agentscope-examples/mcp-native-example/src/main/java/.../CalculatorToolAdapter.java | @Tool-annotated bridge for use from Toolkit/ReActAgent. |
| agentscope-examples/mcp-native-example/src/main/java/.../OpenAiChatTool.java | OkHttp-based OpenAI chat tool; duplicates the Content-Type header. |
| agentscope-examples/mcp-native-example/src/main/java/.../McpServerRunner.java | Stdio MCP server bootstrap; joins on the main thread (deadlocks self) and logs to stdout-sharing JVM. |
| agentscope-examples/mcp-native-example/src/main/java/.../ReActAgentCliRunner.java | CLI agent demo; selects between DashScope and an OpenAI model id that does not exist. |
| agentscope-examples/mcp-native-example/src/test/java/.../CalculatorToolTest.java | Basic JUnit Jupiter tests for the calculator tool. |
| agentscope-examples/mcp-native-example/README.md | Module README for build/run/protocol examples. |
| docs/en/task/mcp-native.md | User-facing guide; contains a Java sample with an invalid throw in a ternary and documents TCP "server" usage that the implementation does not support. |
Comments suppressed due to low confidence (2)
agentscope-core/src/main/java/io/agentscope/core/mcp/server/McpServer.java:20
- This module uses
java.util.logging.Logger(here and inStdioTransport,TcpTransport,MessageRouter,McpServerRunner,CalculatorToolAdapter), but the rest ofagentscope-coreconsistently uses SLF4J (org.slf4j.Logger/LoggerFactory). See e.g.agentscope-core/src/main/java/io/agentscope/core/tool/McpClientManager.java:27-38and dozens of other files. Please switch the new MCP code to SLF4J to stay consistent with the codebase logging convention.
import java.util.logging.Logger;
/**
* Facade for creating and running an MCP server.
*/
public class McpServer {
private static final Logger logger = Logger.getLogger(McpServer.class.getName());
agentscope-core/src/main/java/io/agentscope/core/mcp/schema/Tool.java:36
- The builder methods on this record (and on every other record in
io.agentscope.core.mcp.schema.*—CallToolResult,CallToolRequest,CallToolRequestParams,InitializeResult,InitializeRequest,InitializeRequestParams,ListToolsRequest,ListToolsResult,PaginatedRequestParams,TextContent,Tool) use PascalCase setter names (Description,InputSchema,Name,Content,IsError,Id,Jsonrpc,Method,Params,Cursor, etc.). This violates the Java naming convention used throughout the codebase, where builder setters are camelCase (name(),model(),apiKey(),description()— seeOpenAIChatModel.builder(),Msg.builder(),ReActAgent.builder()usage in this same PR). Please rename the builder methods to camelCase.
public static class Builder {
private Optional<String> description = Optional.empty(); // Optional field
private Map<String, Object> inputSchema = null; // Required field
private String name = null; // Required field
public Builder Description(String value) {
this.description = Optional.of(value);
return this;
}
public Builder InputSchema(Map<String, Object> value) {
this.inputSchema = value;
return this;
}
public Builder Name(String value) {
this.name = value;
return this;
}
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
74126b2 to
7076c84
Compare
…ng, dead code, and add comprehensive tests
…Tool naming, docs syntax, Mockito dependency
AgentScope-Java Version
1.0.12-SNAPSHOT
Description
Adds native MCP server support to AgentScope Java, enabling developers to build and expose custom tool servers via the Model Context Protocol.
Includes complete implementation with StdIO/TCP transports, tool registry, protocol handlers, and a working calculator example with CLI agent integration.
Resolves #229
Background
AgentScope Java previously only supported MCP client integration (connecting to external MCP servers). This PR adds native MCP server support, enabling developers to build and expose their own tool servers via the Model Context Protocol.
Purpose
Enable AgentScope Java applications to:
Changes Made
Core Implementation (
agentscope-core/src/main/java/io/agentscope/core/mcp/)Toolinterface: Server-side tool abstraction with execute(), getName(), getDescription(), getInputSchema()ToolManager: Thread-safe concurrent registry for tool managementInitializeHandler: Implements MCP initialize handshakeListToolsHandler: Implements tools/list discovery endpointCallToolHandler: Implements tools/call execution endpointStdioTransport: JSON-RPC communication via stdin/stdoutTcpTransport: Network-based JSON-RPC communicationMcpServer: Main facade for server creation and tool registrationExample Module (
agentscope-examples/mcp-native-example/)CalculatorTool: MCP tool implementing arithmetic operations (add, subtract, multiply, divide)McpServerRunner: Standalone MCP server exposing calculator toolsReActAgentCliRunner: Interactive CLI agent integrating calculator tools via toolkitCalculatorToolAdapter: AgentScope @tool annotation adapter with loggingDocumentation (
docs/en/task/mcp-native.md)How to Test
Build the modules:
Run MCP Server (StdIO):
Run Interactive CLI Agent:
OPENAI_API_KEY="sk-..." java -cp agentscope-examples/mcp-native-example/target/mcp-native-example.jar \ io.agentscope.examples.mcp.ReActAgentCliRunnerTest with queries:
Run unit tests:
mvn test -pl agentscope-core,agentscope-examples/mcp-native-exampleChecklist
mvn spotless:applymvn test)