{"record":{"id":"d9e5537cadbe4392","repo":"spring-projects/spring-ai","slug":"method-not-found","errorCode":"METHOD_NOT_FOUND","errorMessage":"Sampling not supported","messagePattern":"Sampling not supported","errorType":"error_code","errorClass":"McpError","httpStatus":null,"severity":"error","filePath":"mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/spring/ClientMcpSyncHandlersRegistry.java","lineNumber":106,"sourceCode":"\tpublic McpSchema.ClientCapabilities getCapabilities(String clientName) {\n\t\treturn this.capabilitiesPerClient.getOrDefault(clientName, EMPTY_CAPABILITIES);\n\t}\n\n\t/**\n\t * Invoke the sampling handler for a given MCP client.\n\t *\n\t * @see McpSampling\n\t */\n\tpublic McpSchema.CreateMessageResult handleSampling(String name, McpSchema.CreateMessageRequest samplingRequest) {\n\t\tif (logger.isDebugEnabled()) {\n\t\t\tlogger.debug(\"Handling sampling request for client \" + name);\n\t\t}\n\n\t\tvar handler = this.samplingHandlers.get(name);\n\t\tif (handler != null) {\n\t\t\treturn handler.apply(samplingRequest);\n\t\t}\n\t\tthrow new McpError(new McpSchema.JSONRPCResponse.JSONRPCError(McpSchema.ErrorCodes.METHOD_NOT_FOUND,\n\t\t\t\t\"Sampling not supported\", Map.of(\"reason\", \"Client does not have sampling capability\")));\n\t}\n\n\t/**\n\t * Invoke the elicitation handler for a given MCP client.\n\t *\n\t * @see McpElicitation\n\t */\n\tpublic McpSchema.ElicitResult handleElicitation(String name, McpSchema.ElicitRequest elicitationRequest) {\n\t\tif (logger.isDebugEnabled()) {\n\t\t\tlogger.debug(\"Handling elicitation request for client \" + name);\n\t\t}\n\n\t\tvar handler = this.elicitationHandlers.get(name);\n\t\tif (handler != null) {\n\t\t\treturn handler.apply(elicitationRequest);\n\t\t}\n\t\tthrow new McpError(new McpSchema.JSONRPCResponse.JSONRPCError(McpSchema.ErrorCodes.METHOD_NOT_FOUND,","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/spring/ClientMcpSyncHandlersRegistry.java#L88-L124","documentation":"ClientMcpSyncHandlersRegistry.handleSampling is invoked when the MCP server sends a sampling request to the client. If no sampling handler is registered for the requested client name (samplingHandlers.get(name) == null), the client has no sampling capability, so it responds with an McpError carrying JSON-RPC error code METHOD_NOT_FOUND (-32601) and message \"Sampling not supported\".","triggerScenarios":"A server initiates sampling/createMessage against a client that (a) declared no @McpSampling handler bean, or (b) declared it for a different client name, so samplingHandlers has no entry under the requested name.","commonSituations":"Server asks an LLM via client sampling but the client app never registered sampling support; multi-client setups where the handler is bound to client \"a\" but the request arrives for client \"b\"; handler bean not scanned/not registered in the registry.","solutions":["Register exactly one @McpSampling handler bean for the client so samplingHandlers contains an entry under that client's name.","Verify the client name used when registering the handler matches the name the server uses to address this client.","If sampling is intentionally unsupported, the error is expected — have the server handle the METHOD_NOT_FOUND sampling response gracefully or not request sampling."],"exampleFix":"// before: no sampling handler registered for client \"myClient\"\n// after\n@Bean\nSamplingHandler mySamplingHandler() {\n    return new SamplingHandler(); // method annotated @McpSampling(\"myClient\")\n}","handlingStrategy":"try-catch","validationCode":"// Ensure a sampling handler is registered before connecting the client\nif (!registry.hasSamplingHandler(clientName)) {\n    throw new IllegalStateException(\"No @McpSampling handler registered for client \" + clientName);\n}","typeGuard":null,"tryCatchPattern":"try {\n    server.samplingRequest(...);\n} catch (McpError e) {\n    if (e.getJsonRpcError() != null && e.getJsonRpcError().code() == McpSchema.ErrorCodes.METHOD_NOT_FOUND) {\n        // client has no sampling capability — degrade or skip\n    } else {\n        throw e;\n    }\n}","preventionTips":["Register an @McpSampling handler for every client that servers may sample through.","Match handler client names exactly to client identifiers.","Advertise sampling capability only when a handler exists."],"tags":["mcp","sampling","method-not-found","jsonrpc","capability"],"backgroundTag":"resource-not-found","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}