alpha

Gives a coding assistant rules for building Model Context Protocol servers in Java with the MCP Java SDK 2.x, covering setup, tools, transports and tests.
description: 'Instructions for building Model Context Protocol (MCP) servers in Java with the MCP Java SDK 2.x'
applyTo: '**/*.java, **/pom.xml, **/build.gradle, **/build.gradle.kts'
These instructions apply to the [MCP Java SDK](https://github.com/modelcontextprotocol/java-sdk) 2.x. SDK 2.0.x supports the MCP specification revision [2025-11-25](https://modelcontextprotocol.io/specification/2025-11-25) and the earlier revisions. SDK 3.x will add the revision [2026-07-28](https://modelcontextprotocol.io/specification/2026-07-28).
Scope: Apply these instructions only to projects that use the MCP Java SDK directly. Do not apply them to Quarkus or Quarkiverse MCP servers; use the
quarkus-mcp-server-sseinstruction for those.
io.modelcontextprotocol.sdk:mcp-bom:2.0.1 and add the io.modelcontextprotocol.sdk:mcp artifact.implementation(platform("io.modelcontextprotocol.sdk:mcp-bom:2.0.1")). Also add testRuntimeOnly("org.junit.platform:junit-platform-launcher"). Without it, the test task fails.mcp artifact contains mcp-core and the Jackson 3 JSON mapper mcp-json-jackson3.io.modelcontextprotocol.server, io.modelcontextprotocol.spec, and io.modelcontextprotocol.json. The protocol types are nested in McpSchema.McpServer.sync(transport) or McpServer.async(transport). Set serverInfo(name, version) and capabilities(...), and then call build().Mono) only when the tool code does not block a thread.new StdioServerTransportProvider(McpJsonDefaults.getMapper()).ServerCapabilities.builder().tools(false).resources(false, true).prompts(true).completions().build(). tools(listChanged) and prompts(listChanged) enable list change notifications. resources(subscribe, listChanged) also enables resource subscriptions.SyncToolSpecification.builder().tool(tool).callHandler((exchange, request) -> ...). The handler gets the arguments as a Map from request.arguments().Tool.builder(name, inputSchema), Resource.builder(uri, name), ResourceTemplate.builder(uriTemplate, name), Prompt.builder(name), TextContent.builder(text), and ReadResourceResult.builder(contents). The builders without arguments are deprecated.null. A null value causes an IllegalArgumentException._, -, and .. The server builder rejects an incorrect name with an IllegalArgumentException. addTool() at runtime does not check the name.inputSchema and outputSchema as Map<String, Object> values. The JsonSchema record is deprecated.build() or addTool(). An incorrect schema causes an IllegalArgumentException.inputSchema before it calls the handler. It also validates structuredContent against outputSchema. If the validation fails, the client gets a tool result with isError set to true.outputSchema, return structuredContent in each successful result. Without it, the SDK returns a tool result with isError set to true.outputSchema on the tool and return CallToolResult.builder().structuredContent(map). Also add the same data as text content for older clients.ToolAnnotations.builder().readOnlyHint(true).build(). Use destructiveHint(true) for a destructive tool.CallToolResult.builder().addTextContent(message).isError(true).build() for an error that the model must read.McpError for a tool failure. The client gets a JSON-RPC error, not a tool result. Many hosts do not show this error to the model.McpError.builder(McpSchema.ErrorCodes.INVALID_PARAMS).message(...).build() only for a protocol error, for example an unknown resource URI.SyncResourceSpecification, resource templates with SyncResourceTemplateSpecification, prompts with SyncPromptSpecification, and completions with SyncCompletionSpecification.users://{id}.exchange.progressNotification(...) only when request.progressToken() is not null.exchange.createElicitation(ElicitFormRequest.builder(message, requestedSchema).build()). First, make sure that exchange.getClientCapabilities().elicitation() is not null.ElicitUrlRequest.builder(message, url, elicitationId). First, make sure that exchange.getClientCapabilities().elicitation().url() is not null. Do not send credentials in a form elicitation.exchange.createMessage()). The revision 2026-07-28 deprecates it. Call the LLM provider API directly.exchange.loggingNotification()). The revision 2026-07-28 deprecates it. Log with SLF4J.exchange.listRoots()). The revision 2026-07-28 deprecates it. Get paths from tool arguments, resource URIs, or the server configuration.server.addTool(...) and server.removeTool(name). Then call server.notifyToolsListChanged().System.out.println(). Use slf4j-simple (it writes to stderr), or set the target of the Logback console appender to System.err.HttpServletStreamableServerTransportProvider. For a server without sessions, use HttpServletStatelessServerTransport.Host and Origin headers by default. Always set securityValidator(DefaultServerTransportSecurityValidator.builder()...build()). This also applies to the Spring AI starters.spring-ai-starter-mcp-server-webmvc. Do not use the io.modelcontextprotocol.sdk:mcp-spring-* artifacts.@McpTool and @McpToolParam (from org.springframework.ai.mcp.annotation).Mcp-* request headers. Expose the Mcp-Session-Id response header.maven-shade-plugin. In Gradle, use the application plugin and the start script from installDist. Do not use ./gradlew run as the client command.McpClient.sync(new StdioClientTransport(params, McpJsonDefaults.getMapper())). The SDK has no in-memory transport.description to each property in inputSchema.Mono.fromCallable(...).subscribeOn(Schedulers.boundedElastic()).The snippets do not show all imports. The protocol types are nested in io.modelcontextprotocol.spec.McpSchema. Mono is in reactor.core.publisher, and Schedulers is in reactor.core.scheduler.
import java.util.List;
import java.util.Map;
import io.modelcontextprotocol.json.McpJsonDefaults;
import io.modelcontextprotocol.server.McpServer;
import io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification;
import io.modelcontextprotocol.server.transport.StdioServerTransportProvider;
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
import io.modelcontextprotocol.spec.McpSchema.ServerCapabilities;
import io.modelcontextprotocol.spec.McpSchema.Tool;
public final class McpServerApp {
static SyncToolSpecification addTool() {
Tool tool = Tool.builder("add", Map.of(
"type", "object",
"properties", Map.of(
"a", Map.of("type", "integer", "description", "First number"),
"b", Map.of("type", "integer", "description", "Second number")),
"required", List.of("a", "b")))
.description("Add two integers.")
.build();
return SyncToolSpecification.builder()
.tool(tool)
.callHandler((exchange, request) -> {
long a = ((Number) request.arguments().get("a")).longValue();
long b = ((Number) request.arguments().get("b")).longValue();
return CallToolResult.builder().addTextContent(String.valueOf(a + b)).build();
})
.build();
}
public static void main(String[] args) {
McpServer.sync(new StdioServerTransportProvider(McpJsonDefaults.getMapper()))
.serverInfo("my-server", "0.1.0")
.capabilities(ServerCapabilities.builder().tools(false).build())
.tools(addTool())
.build();
}
}
static SyncToolSpecification wordCountTool() {
Tool tool = Tool.builder("word_count", Map.of(
"type", "object",
"properties", Map.of("text", Map.of("type", "string")),
"required", List.of("text")))
.description("Count the words in a text.")
.outputSchema(Map.of(
"type", "object",
"properties", Map.of("words", Map.of("type", "integer")),
"required", List.of("words")))
.annotations(ToolAnnotations.builder().readOnlyHint(true).build())
.build();
return SyncToolSpecification.builder()
.tool(tool)
.callHandler((exchange, request) -> {
String text = (String) request.arguments().get("text");
int words = text.isBlank() ? 0 : text.trim().split("\\s+").length;
return CallToolResult.builder()
.addTextContent("{\"words\":" + words + "}")
.structuredContent(Map.of("words", words))
.build();
})
.build();
}
static SyncToolSpecification divideTool() {
Tool tool = Tool.builder("divide", Map.of(
"type", "object",
"properties", Map.of("a", Map.of("type", "number"), "b", Map.of("type", "number")),
"required", List.of("a", "b")))
.description("Divide a by b.")
.build();
return SyncToolSpecification.builder()
.tool(tool)
.callHandler((exchange, request) -> {
double a = ((Number) request.arguments().get("a")).doubleValue();
double b = ((Number) request.arguments().get("b")).doubleValue();
if (b == 0) {
return CallToolResult.builder().addTextContent("b must not be zero.").isError(true).build();
}
return CallToolResult.builder().addTextContent(String.valueOf(a / b)).build();
})
.build();
}
static SyncResourceSpecification configResource() {
return new SyncResourceSpecification(
Resource.builder("config://app", "app-config").mimeType("application/json").build(),
(exchange, request) -> ReadResourceResult.builder(List.of(
TextResourceContents.builder(request.uri(), "{\"debug\":false}")
.mimeType("application/json")
.build()))
.build());
}
static SyncResourceTemplateSpecification userResource() {
return new SyncResourceTemplateSpecification(
ResourceTemplate.builder("users://{id}", "user").build(),
(exchange, request) -> {
String id = request.uri().substring("users://".length());
return ReadResourceResult.builder(List.of(
TextResourceContents.builder(request.uri(), "User " + id).build()))
.build();
});
}
Register them with .resources(configResource()) and .resourceTemplates(userResource()).
static SyncPromptSpecification reviewPrompt() {
Prompt prompt = Prompt.builder("code-review")
.description("Review code in a language.")
.arguments(List.of(PromptArgument.builder("language").required(true).build()))
.build();
return new SyncPromptSpecification(prompt, (exchange, request) -> GetPromptResult.builder(List.of(
PromptMessage.builder(Role.USER,
TextContent.builder("Review this " + request.arguments().get("language") + " code.").build())
.build()))
.build());
}
static SyncCompletionSpecification languageCompletion() {
List<String> languages = List.of("java", "kotlin", "scala");
return new SyncCompletionSpecification(new PromptReference("code-review"),
(exchange, request) -> new CompleteResult(new CompleteResult.CompleteCompletion(
languages.stream().filter(l -> l.startsWith(request.argument().value())).toList(), null, false)));
}
Register them with .prompts(reviewPrompt()) and .completions(languageCompletion()).
static AsyncToolSpecification lookupTool() {
Tool tool = Tool.builder("lookup", Map.of(
"type", "object",
"properties", Map.of("query", Map.of("type", "string")),
"required", List.of("query")))
.description("Look up a query in a slow backend.")
.build();
return AsyncToolSpecification.builder()
.tool(tool)
.callHandler((exchange, request) -> {
Object token = request.progressToken();
Mono<Void> progress = token == null ? Mono.empty()
: exchange.progressNotification(
ProgressNotification.builder(token, 0.5).total(1.0).message("Halfway").build());
return progress.then(Mono.fromCallable(() -> slowLookup((String) request.arguments().get("query")))
.subscribeOn(Schedulers.boundedElastic())
.map(text -> CallToolResult.builder().addTextContent(text).build()));
})
.build();
}
Register it on McpServer.async(transport).
HttpServletStreamableServerTransportProvider transport = HttpServletStreamableServerTransportProvider.builder()
.jsonMapper(McpJsonDefaults.getMapper())
.mcpEndpoint("/mcp")
.securityValidator(DefaultServerTransportSecurityValidator.builder()
.allowedHost("localhost:8080")
.allowedOrigin("http://localhost:*")
.build())
.build();
McpServer.sync(transport)
.serverInfo("my-server", "0.1.0")
.capabilities(ServerCapabilities.builder().tools(false).build())
.tools(addTool())
.build();
The transport is a Jakarta Servlet. Register it in a servlet container at /mcp with async support.
@Component
class CalculatorTools {
@McpTool(name = "add", description = "Add two integers")
public int add(@McpToolParam(description = "First number") int a,
@McpToolParam(description = "Second number") int b) {
return a + b;
}
}
class McpServerAppTest {
private static McpSyncClient client;
@BeforeAll
static void startServer() {
String java = Path.of(System.getProperty("java.home"), "bin", "java").toString();
ServerParameters server = ServerParameters.builder(java)
.args("-cp", System.getProperty("java.class.path"), McpServerApp.class.getName())
.build();
client = McpClient.sync(new StdioClientTransport(server, McpJsonDefaults.getMapper())).build();
client.initialize();
}
@AfterAll
static void stopServer() {
client.close();
}
@Test
void add() {
CallToolResult result = client.callTool(
CallToolRequest.builder("add").arguments(Map.of("a", 2, "b", 3)).build());
assertEquals("5", ((TextContent) result.content().get(0)).text());
}
}
© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com


Sea turtle carrying floating film studio island
Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.
image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost
Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.
video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

Cozy Pixel Art Farm Island Village
Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.
image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape
Loading reviews…
Loading comments…