-
Notifications
You must be signed in to change notification settings - Fork 663
feat(mcp): Add native MCP server implementation with example and docs #1432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BEASTSHRIRAM
wants to merge
3
commits into
agentscope-ai:main
Choose a base branch
from
BEASTSHRIRAM:feat/mcp-native-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
agentscope-core/src/main/java/io/agentscope/core/mcp/handler/AbstractMethodHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.agentscope.core.mcp.handler; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.agentscope.core.mcp.message.JsonRpcError; | ||
| import io.agentscope.core.mcp.message.JsonRpcMessage; | ||
| import io.agentscope.core.mcp.message.JsonRpcNotification; | ||
| import io.agentscope.core.mcp.message.JsonRpcRequest; | ||
| import io.agentscope.core.mcp.message.JsonRpcResponse; | ||
| import io.agentscope.core.mcp.transport.TransportException; | ||
|
|
||
| /** | ||
| * Abstract base class for method handlers. | ||
| * | ||
| * <p>Provides common logic for handling requests and notifications. | ||
| */ | ||
| public abstract class AbstractMethodHandler implements MethodHandler { | ||
|
|
||
| protected ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| @Override | ||
| public JsonRpcResponse handleMessage(JsonRpcMessage message) throws TransportException { | ||
| try { | ||
| if (message instanceof JsonRpcRequest) { | ||
| JsonRpcRequest request = (JsonRpcRequest) message; | ||
| Object result = handle(request.getParams()); | ||
| return new JsonRpcResponse(request.getId().orElse(null), result); | ||
| } else if (message instanceof JsonRpcNotification) { | ||
| JsonRpcNotification notification = (JsonRpcNotification) message; | ||
| handle(notification.getParams()); | ||
| return null; // Notifications don't require responses | ||
| } | ||
| throw new TransportException("Unknown message type: " + message.getClass()); | ||
| } catch (TransportException te) { | ||
| throw te; // Re-throw transport exceptions | ||
| } catch (Exception e) { | ||
| if (message instanceof JsonRpcRequest) { | ||
| JsonRpcRequest request = (JsonRpcRequest) message; | ||
| JsonRpcError error = | ||
| new JsonRpcError( | ||
| JsonRpcError.ErrorCode.INTERNAL_ERROR, | ||
| "Internal server error: " + e.getMessage(), | ||
| e.toString()); | ||
| return new JsonRpcResponse(request.getId().orElse(null), error); | ||
| } | ||
| throw new TransportException("Error handling notification", e); | ||
| } | ||
| } | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
agentscope-core/src/main/java/io/agentscope/core/mcp/handler/CallToolHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.agentscope.core.mcp.handler; | ||
|
|
||
| import io.agentscope.core.mcp.schema.CallToolResult; | ||
| import io.agentscope.core.mcp.tool.Tool; | ||
| import io.agentscope.core.mcp.tool.ToolManager; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Handler for `tools/call` requests. Looks up a registered server-side Tool and executes it. | ||
| */ | ||
| public class CallToolHandler extends AbstractMethodHandler { | ||
|
|
||
| private final ToolManager toolManager; | ||
|
|
||
| public CallToolHandler(ToolManager toolManager) { | ||
| this.toolManager = toolManager; | ||
| } | ||
|
|
||
| @Override | ||
| public String getMethod() { | ||
| return "tools/call"; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public Object handle(Object params) throws Exception { | ||
| if (!(params instanceof Map)) { | ||
| throw new IllegalArgumentException("Invalid params for tools/call"); | ||
| } | ||
| Map<String, Object> map = (Map<String, Object>) params; | ||
| String name = (String) map.get("name"); | ||
| Object arguments = map.get("arguments"); | ||
|
|
||
| if (name == null || name.isBlank()) { | ||
| throw new IllegalArgumentException("Tool name is required"); | ||
| } | ||
|
|
||
| Optional<Tool> toolOpt = toolManager.get(name); | ||
| if (toolOpt.isEmpty()) { | ||
| throw new IllegalArgumentException("Tool not found: " + name); | ||
| } | ||
|
|
||
| Tool tool = toolOpt.get(); | ||
| Object result = tool.execute(arguments); | ||
|
|
||
| // Normalize result into a content block list. Always wrap in a text block. | ||
| Map<String, Object> block = new HashMap<>(); | ||
| block.put("type", "text"); | ||
| String text; | ||
| if (result instanceof String) { | ||
| text = (String) result; | ||
| } else { | ||
| text = objectMapper.writeValueAsString(result == null ? "" : result); | ||
| } | ||
| block.put("text", text); | ||
| List<Object> content = new ArrayList<>(); | ||
| content.add(block); | ||
|
|
||
| return new CallToolResult(content, Optional.empty()); | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
agentscope-core/src/main/java/io/agentscope/core/mcp/handler/HandlerRegistry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.agentscope.core.mcp.handler; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| /** | ||
| * Registry for MCP method handlers. | ||
| * | ||
| * <p>Stores and retrieves handlers by method name. | ||
| */ | ||
| public class HandlerRegistry { | ||
|
|
||
| private final Map<String, MethodHandler> handlers = new ConcurrentHashMap<>(); | ||
|
|
||
| /** | ||
| * Register a handler for a method. | ||
| * | ||
| * @param method the method name | ||
| * @param handler the handler | ||
| */ | ||
| public void register(String method, MethodHandler handler) { | ||
| handlers.put(method, handler); | ||
| } | ||
|
|
||
| /** | ||
| * Get a handler for a method. | ||
| * | ||
| * @param method the method name | ||
| * @return the handler, or empty if not found | ||
| */ | ||
| public Optional<MethodHandler> get(String method) { | ||
| return Optional.ofNullable(handlers.get(method)); | ||
| } | ||
|
|
||
| /** | ||
| * Check if a handler is registered for a method. | ||
| * | ||
| * @param method the method name | ||
| * @return true if handler exists | ||
| */ | ||
| public boolean has(String method) { | ||
| return handlers.containsKey(method); | ||
| } | ||
|
|
||
| /** | ||
| * Unregister a handler for a method. | ||
| * | ||
| * @param method the method name | ||
| */ | ||
| public void unregister(String method) { | ||
| handlers.remove(method); | ||
| } | ||
|
|
||
| /** | ||
| * Get all registered method names. | ||
| * | ||
| * @return set of method names | ||
| */ | ||
| public Map<String, MethodHandler> getAll() { | ||
| return new ConcurrentHashMap<>(handlers); | ||
| } | ||
|
|
||
| /** | ||
| * Clear all handlers. | ||
| */ | ||
| public void clear() { | ||
| handlers.clear(); | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
agentscope-core/src/main/java/io/agentscope/core/mcp/handler/InitializeHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.agentscope.core.mcp.handler; | ||
|
|
||
| import io.agentscope.core.mcp.schema.InitializeResult; | ||
| import io.agentscope.core.mcp.tool.ToolManager; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Handler for `initialize` requests (handshake). | ||
| */ | ||
| public class InitializeHandler extends AbstractMethodHandler { | ||
|
|
||
| private final ToolManager toolManager; | ||
|
|
||
| public InitializeHandler(ToolManager toolManager) { | ||
| this.toolManager = toolManager; | ||
| } | ||
|
|
||
| @Override | ||
| public String getMethod() { | ||
| return "initialize"; | ||
| } | ||
|
|
||
| @Override | ||
| public Object handle(Object params) throws Exception { | ||
| Map<String, Object> capabilities = new HashMap<>(); | ||
| capabilities.put("tools", new HashMap<>()); | ||
| capabilities.put("protocol", "mcp"); | ||
|
|
||
| Map<String, Object> serverInfo = new HashMap<>(); | ||
| serverInfo.put("name", "agentscope-core"); | ||
| serverInfo.put("version", "0.1.0"); | ||
|
|
||
| return new InitializeResult(capabilities, "2024-11-05", serverInfo); | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
agentscope-core/src/main/java/io/agentscope/core/mcp/handler/ListToolsHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.agentscope.core.mcp.handler; | ||
|
|
||
| import io.agentscope.core.mcp.schema.ListToolsResult; | ||
| import io.agentscope.core.mcp.tool.Tool; | ||
| import io.agentscope.core.mcp.tool.ToolManager; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Handler for `tools/list` requests. Returns metadata about registered tools. | ||
| */ | ||
| public class ListToolsHandler extends AbstractMethodHandler { | ||
|
|
||
| private final ToolManager toolManager; | ||
|
|
||
| public ListToolsHandler(ToolManager toolManager) { | ||
| this.toolManager = toolManager; | ||
| } | ||
|
|
||
| @Override | ||
| public String getMethod() { | ||
| return "tools/list"; | ||
| } | ||
|
|
||
| @Override | ||
| public Object handle(Object params) throws Exception { | ||
| List<Object> out = new ArrayList<>(); | ||
| for (Tool t : toolManager.list()) { | ||
| Map<String, Object> m = new HashMap<>(); | ||
| m.put("name", t.getName()); | ||
| m.put("description", t.getDescription()); | ||
| m.put("inputSchema", t.getInputSchema()); | ||
| out.add(m); | ||
| } | ||
|
|
||
| return new ListToolsResult(Optional.empty(), out); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.