<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ayush Soni</title>
    <description>The latest articles on DEV Community by Ayush Soni (@ayushwing).</description>
    <link>https://dev.to/ayushwing</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4210786%2F105aad99-2265-499a-8c6f-fa58f937f3cb.png</url>
      <title>DEV Community: Ayush Soni</title>
      <link>https://dev.to/ayushwing</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ayushwing"/>
    <language>en</language>
    <item>
      <title>Building an annotation-driven MCP server in Java</title>
      <dc:creator>Ayush Soni</dc:creator>
      <pubDate>Tue, 27 Oct 2026 11:45:16 +0000</pubDate>
      <link>https://dev.to/ayushwing/building-an-annotation-driven-mcp-server-in-java-39mo</link>
      <guid>https://dev.to/ayushwing/building-an-annotation-driven-mcp-server-in-java-39mo</guid>
      <description>&lt;p&gt;Exposing a Java method to an AI agent should take one annotation. When I started using MCP servers daily with Claude Code and Cursor, I noticed that almost every example was written in Python or TypeScript — while most of the systems I'd want an agent to call are Spring Boot services.&lt;/p&gt;

&lt;p&gt;So I wrote &lt;a href="https://github.com/ayushwing/java-mcp-server" rel="noopener noreferrer"&gt;java-mcp-server&lt;/a&gt;: a small toolkit where this is the whole integration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyTools&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@McpTool&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"greet"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Say hello to someone"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@McpParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Person's name"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello, "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@McpResource&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"config://app/{key}"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Read a config value"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@McpParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"key"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"(not set)"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start the app, and any MCP client can list and call &lt;code&gt;greet&lt;/code&gt; and read &lt;code&gt;config://app/java.version&lt;/code&gt;. This post covers how it works, the decisions behind it, and the parts I'd change.&lt;/p&gt;

&lt;h2&gt;
  
  
  MCP in one paragraph
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://modelcontextprotocol.io" rel="noopener noreferrer"&gt;Model Context Protocol&lt;/a&gt; is a JSON-RPC 2.0 protocol between an AI client (Claude Desktop, Claude Code, Cursor) and a server that offers &lt;strong&gt;tools&lt;/strong&gt; (functions the model may call), &lt;strong&gt;resources&lt;/strong&gt; (read-only data addressed by URI) and prompts. A session starts with &lt;code&gt;initialize&lt;/code&gt;, the client asks &lt;code&gt;tools/list&lt;/code&gt;, and when the model decides to use a tool the client sends &lt;code&gt;tools/call&lt;/code&gt; with a name and JSON arguments. That's the core loop — and it maps almost one-to-one onto "find a method, describe it, invoke it".&lt;/p&gt;

&lt;h2&gt;
  
  
  The shape of the toolkit
&lt;/h2&gt;

&lt;p&gt;I split the project into four Maven modules, and the split turned out to be the most useful decision in the project:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Module&lt;/th&gt;
&lt;th&gt;What it holds&lt;/th&gt;
&lt;th&gt;Depends on Spring?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;mcp-core&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Annotations (&lt;code&gt;@McpTool&lt;/code&gt;, &lt;code&gt;@McpResource&lt;/code&gt;, &lt;code&gt;@McpParam&lt;/code&gt;) and the JSON-RPC / tool models&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;mcp-server-spring-boot&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Auto-configuration, bean scanning, the &lt;code&gt;/mcp&lt;/code&gt; HTTP endpoint&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;mcp-server-stdio&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Newline-delimited JSON-RPC over stdin/stdout, for Claude Desktop&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;mcp-server-sample&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A runnable app with system-info and math tools&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Keeping &lt;code&gt;mcp-core&lt;/code&gt; free of Spring meant the stdio transport — which Claude Desktop uses when it launches a server as a child process — was a small module on top of the same models, not a second implementation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn7vjim8zhf9j5wj79ioi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn7vjim8zhf9j5wj79ioi.png" alt="An MCP client calls POST /mcp; the controller routes JSON-RPC methods to a tool registry and a resource registry, which invoke annotated Spring bean methods." width="800" height="264"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;One endpoint, two registries, plain Spring beans underneath. &lt;a href="https://ayushwing.com/diagrams/java-mcp-server.html" rel="noopener noreferrer"&gt;Explore the interactive diagram&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: turn methods into tool definitions
&lt;/h2&gt;

&lt;p&gt;At startup, &lt;code&gt;McpToolRegistry&lt;/code&gt; walks every bean in the &lt;code&gt;ApplicationContext&lt;/code&gt;, looks for methods carrying &lt;code&gt;@McpTool&lt;/code&gt;, and builds a &lt;code&gt;ToolDefinition&lt;/code&gt; for each one. The interesting part is the input schema, because that's what the model reads to decide how to call your method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Parameter&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getParameters&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;McpParam&lt;/span&gt; &lt;span class="n"&gt;mp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAnnotation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;McpParam&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;paramName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mp&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;paramDesc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mp&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;isRequired&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mp&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;mp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paramName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ParameterSchema&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getType&lt;/span&gt;&lt;span class="o"&gt;()),&lt;/span&gt; &lt;span class="n"&gt;paramDesc&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isRequired&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paramName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Java types map to JSON Schema types (&lt;code&gt;int&lt;/code&gt;/&lt;code&gt;long&lt;/code&gt; → &lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;double&lt;/code&gt; → &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt; → &lt;code&gt;boolean&lt;/code&gt;, everything else → &lt;code&gt;string&lt;/code&gt;). It's deliberately simple. I found that &lt;strong&gt;descriptions matter more than types&lt;/strong&gt;: a model given &lt;code&gt;name: string&lt;/code&gt; guesses; a model given "Person's name, as it should appear in the greeting" gets it right.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: one endpoint, a switch on the method
&lt;/h2&gt;

&lt;p&gt;Every request arrives at a single &lt;code&gt;POST /mcp&lt;/code&gt; (configurable via &lt;code&gt;mcp.endpoint&lt;/code&gt;). The controller is a switch over the JSON-RPC method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"initialize"&lt;/span&gt;     &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handleInitialize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"tools/list"&lt;/span&gt;     &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handleToolsList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"tools/call"&lt;/span&gt;     &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handleToolsCall&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getParams&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"resources/list"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handleResourcesList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"resources/read"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handleResourcesRead&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getParams&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"notifications/initialized"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
         &lt;span class="s"&gt;"notifications/cancelled"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;JsonRpcResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;success&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;JsonRpcResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ERR_METHOD_NOT_FOUND&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Unknown method: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Errors use the JSON-RPC codes clients expect: &lt;code&gt;-32601&lt;/code&gt; for an unknown method, &lt;code&gt;-32602&lt;/code&gt; for bad params (a missing tool name, an unknown tool, a URI that matches no resource), &lt;code&gt;-32603&lt;/code&gt; for anything that blew up inside.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: invoke, and never throw at the model
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;McpToolInvoker&lt;/code&gt; maps each JSON argument onto the Java parameter type with Jackson's &lt;code&gt;treeToValue&lt;/code&gt;, calls the method reflectively, and wraps the return value: strings pass through, anything else is serialised to JSON.&lt;/p&gt;

&lt;p&gt;The rule I settled on: &lt;strong&gt;a failing tool returns a tool error, not a protocol error.&lt;/strong&gt; If your method throws, the client receives a &lt;code&gt;ToolResult&lt;/code&gt; flagged as an error with the cause's message. The model can read that and recover ("the divisor can't be zero, let me ask the user"), which it can't do with a transport-level failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources: a tiny URI-template matcher
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;@McpResource(uri = "config://app/{key}")&lt;/code&gt; registers a template. When a client reads &lt;code&gt;config://app/java.version&lt;/code&gt;, the registry splits both on &lt;code&gt;/&lt;/code&gt;, matches literal segments, and captures &lt;code&gt;{variables}&lt;/code&gt; into a map that's passed to the method. It's twenty lines and handles the common cases; it doesn't do wildcards or query strings, and I haven't needed them yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two transports, one handler
&lt;/h2&gt;

&lt;p&gt;HTTP is the natural fit for a service already running in your infrastructure. Claude Desktop, though, launches MCP servers as child processes and speaks newline-delimited JSON-RPC over stdin/stdout. &lt;code&gt;McpStdioRunner&lt;/code&gt; is a read-line loop around the same request handler, with two details that bit me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Logs must go to stderr.&lt;/strong&gt; Anything written to stdout that isn't a JSON-RPC message corrupts the stream.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Notifications get no response.&lt;/strong&gt; A message without an &lt;code&gt;id&lt;/code&gt; must not produce a reply, or strict clients drop the connection.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I'd do differently
&lt;/h2&gt;

&lt;p&gt;This is a v0.1, and three things are already on my list:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Proxied beans hide annotations.&lt;/strong&gt; The registry reads &lt;code&gt;bean.getClass().getMethods()&lt;/code&gt;. If the bean is wrapped in a CGLIB proxy — say because it has &lt;code&gt;@Transactional&lt;/code&gt; — the proxy's overriding methods don't carry &lt;code&gt;@McpTool&lt;/code&gt;, and the tool silently disappears. The fix is to resolve the target class with &lt;code&gt;AopUtils.getTargetClass()&lt;/code&gt; and use &lt;code&gt;AnnotationUtils.findAnnotation&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Missing arguments become &lt;code&gt;null&lt;/code&gt;.&lt;/strong&gt; That's fine for &lt;code&gt;String&lt;/code&gt;, but a missing value for an &lt;code&gt;int&lt;/code&gt; parameter fails at invoke time. Validate against the &lt;code&gt;required&lt;/code&gt; list before calling and return a clear &lt;code&gt;-32602&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parameter names need &lt;code&gt;-parameters&lt;/code&gt;.&lt;/strong&gt; Without &lt;code&gt;@McpParam&lt;/code&gt;, names come from reflection, which yields &lt;code&gt;arg0&lt;/code&gt; unless the code is compiled with the &lt;code&gt;-parameters&lt;/code&gt; flag. Either fail fast at startup or require &lt;code&gt;@McpParam&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Keep the protocol core independent of your framework; transports become cheap.&lt;/li&gt;
&lt;li&gt;Spend your effort on descriptions and schemas — that's the interface the model actually reads.&lt;/li&gt;
&lt;li&gt;Return tool failures as data the model can reason about.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code, tests and sample app are on &lt;a href="https://github.com/ayushwing/java-mcp-server" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. If you're putting internal Java services in front of agents, I'd like to hear how you're handling auth — that's the next big piece.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://ayushwing.com/blog/java-mcp-server" rel="noopener noreferrer"&gt;ayushwing.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>ai</category>
      <category>mcp</category>
    </item>
  </channel>
</rss>
