ClaudeCode.MCP.Router (ClaudeCode v0.20.0)

View Source

Dispatches JSONRPC requests to in-process MCP tool server modules.

Handles the MCP protocol methods (initialize, tools/list, tools/call) by routing to the appropriate tool module's execute/2 callback.

This module is called by the adapter when it receives an mcp_message control request from the CLI for a type: "sdk" server.

Summary

Functions

Handles a JSONRPC request for the given tool server module.

Functions

handle_request(server_module, message, assigns \\ %{})

@spec handle_request(module(), map(), map()) :: map()

Handles a JSONRPC request for the given tool server module.

Returns a JSONRPC response map ready for JSON encoding.

Parameters

  • server_module - A module that uses ClaudeCode.MCP.Server and exports __tool_server__/0
  • message - A decoded JSONRPC request map with "method" key
  • assigns - Optional map of assigns to set on the Hermes frame (available to tools that define execute/2)

Supported Methods

  • "initialize" - Returns protocol version, capabilities, and server info
  • "notifications/initialized" - Acknowledges initialization (returns empty result)
  • "tools/list" - Returns all registered tools with their schemas
  • "tools/call" - Dispatches to the named tool's execute/2 callback

Examples

iex> message = %{"jsonrpc" => "2.0", "id" => 1, "method" => "initialize", "params" => %{}}
iex> Router.handle_request(MyApp.Tools, message)
%{"jsonrpc" => "2.0", "id" => 1, "result" => %{"protocolVersion" => "2024-11-05", ...}}