{"slug": "building-an-mcp-server-using-spring-ai-json-rpc-and-sse-server-sent-events", "title": "Building an MCP Server Using Spring AI, JSON-RPC and SSE (Server-Sent Events)", "summary": "A developer built an MCP (Model Context Protocol) server using Spring AI with JSON-RPC and Server-Sent Events (SSE) transport. The implementation enables standardized tool exposure for AI agents by leveraging Spring Boot's auto-configuration to create SSE endpoints and JSON-RPC message handling. The server architecture automatically discovers and registers tools through `@Tool` annotations, wrapping them in `ToolCallback` objects for the MCP tool registry.", "body_md": "**Introduction:**\n\nModern LLM-powered applications require external tools to interact with real-systems such as a databases, APIs, cloud platforms, and enterprise services. MCP (Model context Protocol) provides standardized mechanism for exposing tools to AI agents.\n\nIn this article, we will build an MCP Server using Spring AI with SSE( Server-Sent Events) transport support. We will also understand how JSON-RPC and Server-Sent Events work together to enable asynchronous communication between AI agents and tools.\n\n**What is MCP?**\n\nMCP (Model Context Protocol) is a protocol designed to expose tools, resources, and capabilities to LLM-powered applications in a standardized way.\n\nAn MCP server acts as a tool provider, while an MCP client acts as a Consumer.\n\nThe protocol enables:\n\n**Why JSON-RPC**\n\nMCP( Model Context Protocol) uses JSON-RPC as the communication protocol.\n\nJSON-RPC provides:\n\nExample request:\n\n{\n\n\"jsonrpc\":\"2.0\",\n\n\"id\":\"101\",\n\n\"method\":\"tools/call\",\n\n\"params\":{\n\n\"name\":\"getWeather\",\n\n\"arguments\":{\n\n\"city\":\"Atlanta\"\n\n}\n\n}\n\n}\n\n**Why SSE Transport?**\n\nTraditional HTTP request-response communication is insufficient for long-running AI workflows.\n\nSSE (Server-sent Events) enables:\n\nIn MCP architecture:\n\n**Spring AI MCP Server Architecture :**\n\nThe MCP Server Contains:\n\n**Implementing MCP Server Using Spring AI**\n\nFolder structure\n\nInclude below dependencies pom.xml\n\n**What happens internally when Spring AI MCP Server Starts?**\n\nConsider the dependency:\n\norg.springframework.ai\n\nspring-ai-starter-mcp-server-webmvc\n\nAt first glance it look like a simple starter dependency, but internally Spring boot performs several steps to transform your application into an MCP-compliant server.\n\n**Step 1: Spring Boot Starts**\n\nWhen the application starts: SpringApplication.run(Application.class, args);\n\nSpring boot begins its bootstrap process.\n\nDuring bootstrap it scans:\n\nMETA-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports\n\nInside every starter dependency.\n\n**Step 2: MCP Auto Configuration is Discovered**\n\nThe MCP starter contributes auto-configuration classes.\n\nConceptually: Spring-ai-starter-mcp-server-webmvc is McpWebMvcServerTransportAutoConfiguration.\n\nSpring Boot automatically imports these configurations into application Context.\n\nAt this stage Spring creates infrastructure beans required by:\n\nNo application code has run yet.\n\n**Step 3: Defining Tools**\n\nSpring AI MCP server exposes tools using:\n\n@Tool\n\n@ToolParam\n\n**Step 4: Registering Tool**\n\nThis is the most important part.\n\nSpring AI does not invoke methods directly from JSON-RPC requests. Instead it wraps each discovered tool in to a ToolCallback\n\nThe tools are registered through: MethodToolCallbackProvider\n\nThese tools become available in the MCP tool Registry.\n\n**Step 5: SSE Endpoint is Created**\n\nThe MCP auto-configuration also creates infrastructure for SSE Transport.\n\nConceptually: GET /sse. This endpoint maintains long-lived connections.\n\nClient: GET /sse. Connection remains open. Spring internally creates SseEmitter objects for connected clients.\n\nExample: Connected clients\n\nClient A -> SseEmitter\n\nClient B -> SseEmitter\n\nClient C -> SseEmittter.\n\nThese emitters are retained and reused whenever events need to be published.\n\n**Step 6: JSON-RPC Endpoint is Created**\n\nThe MCP starter also exposes POST /mcp/message. This endpoint accepts JSON-RPC messages.\n\nExample:\n\n{\n\n\"jsonrpc\":\"2.0\",\n\n\"id\":3,\n\n\"method\":\"tools/call\",\n\n\"params\":{\n\n\"name\":\"calculate_discount\",\n\n\"arguments\":{\n\n\"originalPrice\":100,\n\n\"discountPercentage\":20\n\n}\n\n}\n\n}\n\n**Step 7: Request Arrives**\n\nClient sends: POST /mcp/message. Spring MCV dispatches request to MCP Controller.\n\n**Conceptually:** DispatcherServlet -> MCP Controller.\n\nThe MCP Controller Parses **method** = tools/call, **toolName** = calculateDiscount and **arguments** = {…}.\n\nThe Controller queries Tool Registry.\n\n**Conceptually**: ToolCallback **callback** = registry.find(“calculateDiscount”);\n\n**Result**: calculateDiscountCallback.\n\nThe callback executes underlying method.\n\n**conceptually**: callback.call(argument) which internally invokes calculateDiscount method and Tool execution Occurs.\n\n**Step 8: JSON-RPC Response Creation**\n\nFramework builds:\n\n{ \"jsonrpc\":\"2.0\", \"id\":3, \"result\":{\"content\":[{\"type\":\"text\",\"text\":\"\\\"Original Price: $100.00, Discount: 20.0%, you Save: $20.00, Final Price: $80.00\\\"\"}]} .\n\nNotice: id = 3 is preserved. This ID is critical for request-response correlation.\n\n**Step 9: SSE Publication**\n\nInstead of returning the response directly through the Original HTTP request, the framework publishes the response through the active SSE channel.\n\n**Conceptually:**\n\nSseEmitter.send(responseMessage);\n\n**Result:**\n\n{ \"jsonrpc\":\"2.0\", \"id\":3, \"result\":{\"content\":[{\"type\":\"text\",\"text\":\"\\\"Original Price: $100.00, Discount: 20.0%, you Save: $20.00, Final Price: $80.00\\\"\"}]} } is streamed to connected Client.\n\n**Step 10: Client Receives Event**\n\nThe MCP Client SSE Listener Thread receives: { \"jsonrpc\":\"2.0\", \"id\":3, \"result\":{\"content\":[{\"type\":\"text\",\"text\":\"\\\"Original Price: $100.00, Discount: 20.0%, you Save: $20.00, Final Price: $80.00\\\"\"}]} }\n\nThe listener\n\n**extracts**: id=3\n\n**Looks up**: ConcurrentHashMap< Integer, CompletableFuture> pendingRequest\n\n**Finds** : future = pendingRequest.remove(3)\n\n**Then**: future.complete(response)\n\nThe waiting caller thread wakes up.\n\n**Execution:**\n\nExecute HttpMcpServerApplication.java to bootstrap the Spring Boot application, Which hosts the MCP Server and listens on port 8090.\n\nDuring the spring boot bootstrap process, the MCP auto-configuration scans for tool definitions, create too callbacks, and registers then with MCP server’s tool registry.\n\nThe MCP auto-configuration also creates infrastructure for SSE Transport. GET **/sse ** endpoint maintains long-lived connections\n\nWhen the MCP client connects to the /sse endpoint, The MCP server establishes a Server-Sent Events (SSE) stream and returns an endpoint event containing a unique session identifier as shown in above figure.\n\nThe client extracts the sessionId from the endpoint URL and include it in all subsequent HTTP POST requests to the\n\n/mcp/message endpoint, including:\n\nThe sessionId uniquely identifies a client session and enables the MCP server to correlate requests, responses, and asynchronous events belong to same client.\n\nThis mechanism forms the foundation of stateful and asynchronous communication between MCP Client and MCP Server when using the SSE transport.\n\nAccording to the MCP protocol lifecycle, the expected life cycle is\n\nThe purpose of initialize is to negotiate capabilities and protocol versions between client and server. The notification/initialization message tells the server that the client has completed initialization and is ready for normal operations.\n\n**Initialize:**\n\nThe MCP client sends an HTTP POST request containing the JSON-RPC initialize message.\n\nUpon processing the request, The MCP Server publishes the corresponding JSON-RPC response asynchronous over the established /sse event stream.\n\nThe MCP client sends an HTTP POST request containing the JSON-RPC notification/initialized message.\n\nThe MCP client sends an HTTP POST request containing the JSON-RPC tools/list message.\n\nUpon processing the request, The MCP Server publishes the corresponding JSON-RPC response asynchronous over the established /sse event stream.\n\nThe MCP client sends an HTTP POST request containing the JSON-RPC tools/list message.\n\nUpon processing the request, The MCP Server publishes the corresponding JSON-RPC response asynchronous over the established /sse event stream.", "url": "https://wpnews.pro/news/building-an-mcp-server-using-spring-ai-json-rpc-and-sse-server-sent-events", "canonical_source": "https://dev.to/anil_lalam_2cee9c52a20a39/building-an-mcp-server-using-spring-ai-json-rpc-and-sse-server-sent-events-5i3", "published_at": "2026-05-31 13:05:19+00:00", "updated_at": "2026-05-31 13:42:40.116545+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-agents", "ai-tools", "ai-infrastructure"], "entities": ["Spring AI", "MCP", "JSON-RPC", "SSE", "Model Context Protocol"], "alternates": {"html": "https://wpnews.pro/news/building-an-mcp-server-using-spring-ai-json-rpc-and-sse-server-sent-events", "markdown": "https://wpnews.pro/news/building-an-mcp-server-using-spring-ai-json-rpc-and-sse-server-sent-events.md", "text": "https://wpnews.pro/news/building-an-mcp-server-using-spring-ai-json-rpc-and-sse-server-sent-events.txt", "jsonld": "https://wpnews.pro/news/building-an-mcp-server-using-spring-ai-json-rpc-and-sse-server-sent-events.jsonld"}}