Building an MCP Server
The MCP::Server class is the core component that handles JSON-RPC requests and responses. It implements the Model Context Protocol specification, handling model context requests and responses.
Key Features
- Implements JSON-RPC 2.0 message handling
- Supports protocol initialization and capability negotiation
- Manages tool registration and invocation
- Supports prompt registration and execution
- Supports resource registration and retrieval
- Supports stdio and Streamable HTTP (including SSE) transports
- Supports notifications for list changes (tools, prompts, resources)
- Supports roots (server-to-client filesystem boundary queries)
- Supports sampling (server-to-client LLM completion requests)
- Supports cursor-based pagination for list operations
- Supports cancellation of in-flight requests on both server and client (notifications/cancelled)
Supported Methods
initialize- Initializes the protocol and returns server capabilitiesserver/discover- Sessionless capability discovery (MCP 2026-07-28, SEP-2575): returns the server’s capabilities beforeinitializeand without anMcp-Session-Id, and anchors the stateless modern lifecycle; see Discoverysubscriptions/listen- Long-lived notification subscription stream (MCP 2026-07-28, SEP-2575), replacing the legacy HTTP GET listening stream; see Notification Subscriptions- Multi round-trip
input_requiredresults (MCP 2026-07-28, SEP-2322): handlers returnMCP::Server::InputRequiredResultto ask the client for additional input instead of performing a server-initiated request; see Multi-Round-Trip Results ping- Simple health checklogging/setLevel- Configures the minimum log level for the servertools/list- Lists all registered tools and their schemastools/call- Invokes a specific tool with provided argumentsprompts/list- Lists all registered prompts and their schemasprompts/get- Retrieves a specific prompt by nameresources/list- Lists all registered resources and their schemasresources/read- Retrieves a specific resource by nameresources/templates/list- Lists all registered resource templates and their schemasresources/subscribe- Subscribes to updates for a specific resourceresources/unsubscribe- Unsubscribes from updates for a specific resourcecompletion/complete- Returns autocompletion suggestions for prompt arguments and resource URIsroots/list- Requests filesystem roots from the client (server-to-client)sampling/createMessage- Requests LLM completion from the client (server-to-client)elicitation/create- Requests user input from the client (server-to-client)