public class McpServer extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_MESSAGE_ENDPOINT
Default message endpoint path as specified by the MCP transport specification.
|
static String |
DEFAULT_SSE_ENDPOINT
Default SSE endpoint path as specified by the MCP transport specification.
|
static String |
ENDPOINT_EVENT_TYPE
Event type for sending the message endpoint URI to clients.
|
| Constructor and Description |
|---|
McpServer() |
McpServer(String sseEndpoint,
String messageEndpoint) |
| Modifier and Type | Method and Description |
|---|---|
McpServer |
capabilities(McpServerCapabilities serverCapabilities)
Sets the server capabilities that will be advertised to clients during
connection initialization.
|
String |
getMessageEndpoint() |
String |
getSseEndpoint() |
McpServer |
prompts(List<McpPromptSpecification> prompts)
Registers multiple prompts with their handlers using a List.
|
McpServer |
prompts(Map<String,McpPromptSpecification> prompts)
Registers multiple prompts with their handlers using a Map.
|
McpServer |
prompts(McpPromptSpecification... prompts)
Registers multiple prompts with their handlers using varargs.
|
McpServer |
resources(List<McpResourceSpecification> resourceSpecifications)
Registers multiple resources with their handlers using a List.
|
McpServer |
resources(Map<String,McpResourceSpecification> resourceSpecifications)
Registers multiple resources with their handlers using a Map.
|
McpServer |
resources(McpResourceSpecification... resourceSpecifications)
Registers multiple resources with their handlers using varargs.
|
McpServer |
resourceTemplates(List<McpResourceTemplateSpecification> resourceTemplates)
Sets the resource templates that define patterns for dynamic resource access.
|
McpServer |
resourceTemplates(McpResourceTemplateSpecification... resourceTemplates)
Sets the resource templates using varargs for convenience.
|
McpServer |
rootsChangeHandler(BiConsumer<McpServerSession,List<McpRoot>> handler)
Registers a consumer that will be notified when the list of roots changes.
|
McpServer |
rootsChangeHandlers(BiConsumer<McpServerSession,List<McpRoot>>... handlers)
Registers multiple consumers that will be notified when the list of roots
changes using varargs.
|
McpServer |
rootsChangeHandlers(List<BiConsumer<McpServerSession,List<McpRoot>>> handlers)
Registers multiple consumers that will be notified when the list of roots
changes.
|
void |
sendHeartbeat()
发送心跳
|
McpServer |
serverInfo(McpImplementation serverInfo)
Sets the server implementation information that will be shared with clients
during connection initialization.
|
McpServer |
serverInfo(String name,
String version)
Sets the server implementation information using name and version strings.
|
HttpResponse |
sseEndpoint(HttpRequest request)
sse endpoint
|
HttpResponse |
sseMessageEndpoint(HttpRequest request)
sse message endpoint
|
McpServer |
tool(McpTool tool,
BiFunction<McpServerSession,Map<String,Object>,McpCallToolResult> handler)
Adds a single tool with its implementation handler to the server.
|
McpServer |
tools(List<McpToolSpecification> toolSpecifications)
Adds multiple tools with their handlers to the server using a List.
|
McpServer |
tools(McpToolSpecification... toolSpecifications)
Adds multiple tools with their handlers to the server using varargs.
|
public static final String ENDPOINT_EVENT_TYPE
public static final String DEFAULT_SSE_ENDPOINT
public static final String DEFAULT_MESSAGE_ENDPOINT
public McpServer serverInfo(McpImplementation serverInfo)
serverInfo - The server implementation details including name and version.
Must not be null.IllegalArgumentException - if serverInfo is nullpublic McpServer serverInfo(String name, String version)
serverInfo(McpImplementation).name - The server name. Must not be null or empty.version - The server version. Must not be null or empty.IllegalArgumentException - if name or version is null or emptyserverInfo(McpImplementation)public McpServer capabilities(McpServerCapabilities serverCapabilities)
serverCapabilities - The server capabilities configuration. Must not be
null.IllegalArgumentException - if serverCapabilities is nullpublic McpServer tool(McpTool tool, BiFunction<McpServerSession,Map<String,Object>,McpCallToolResult> handler)
McpToolSpecification explicitly.
Example usage:
.tool(
new Tool("calculator", "Performs calculations", schema),
(exchange, args) -> new CallToolResult("Result: " + calculate(args))
)
tool - The tool definition including name, description, and schema. Must
not be null.handler - The function that implements the tool's logic. Must not be null.
The function's first argument is an McpServer upon which
the server can interact with the connected client. The second argument is the
list of arguments passed to the tool.IllegalArgumentException - if tool or handler is nullpublic McpServer tools(List<McpToolSpecification> toolSpecifications)
toolSpecifications - The list of tool specifications to add. Must not be
null.IllegalArgumentException - if toolSpecifications is nulltools(McpToolSpecification...)public McpServer tools(McpToolSpecification... toolSpecifications)
Example usage:
.tools(
new ToolSpecification(calculatorTool, calculatorHandler),
new ToolSpecification(weatherTool, weatherHandler),
new ToolSpecification(fileManagerTool, fileManagerHandler)
)
toolSpecifications - The tool specifications to add. Must not be null.IllegalArgumentException - if toolSpecifications is nulltools(List)public McpServer resources(Map<String,McpResourceSpecification> resourceSpecifications)
resourceSpecifications - Map of resource name to specification. Must not
be null.IllegalArgumentException - if resourceSpecifications is nullresources(McpResourceSpecification...)public McpServer resources(List<McpResourceSpecification> resourceSpecifications)
resourceSpecifications - List of resource specifications. Must not be
null.IllegalArgumentException - if resourceSpecifications is nullresources(McpResourceSpecification...)public McpServer resources(McpResourceSpecification... resourceSpecifications)
Example usage:
.resources(
new ResourceSpecification(fileResource, fileHandler),
new ResourceSpecification(dbResource, dbHandler),
new ResourceSpecification(apiResource, apiHandler)
)
resourceSpecifications - The resource specifications to add. Must not be
null.IllegalArgumentException - if resourceSpecifications is nullpublic McpServer resourceTemplates(List<McpResourceTemplateSpecification> resourceTemplates)
Example usage:
.resourceTemplates(
new ResourceTemplate("file://{path}", "Access files by path"),
new ResourceTemplate("db://{table}/{id}", "Access database records")
)
resourceTemplates - List of resource templates. If null, clears existing
templates.IllegalArgumentException - if resourceTemplates is null.#resourceTemplates(List...) public McpServer resourceTemplates(McpResourceTemplateSpecification... resourceTemplates)
resourceTemplates(List).resourceTemplates - The resource templates to set.IllegalArgumentException - if resourceTemplates is nullresourceTemplates(List)public McpServer prompts(Map<String,McpPromptSpecification> prompts)
Example usage:
Map<String, PromptSpecification> prompts = new HashMap<>();
prompts.put("analysis", new PromptSpecification(
new Prompt("analysis", "Code analysis template"),
(exchange, request) -> new GetPromptResult(generateAnalysisPrompt(request))
));
.prompts(prompts)
prompts - Map of prompt name to specification. Must not be null.IllegalArgumentException - if prompts is nullpublic McpServer prompts(List<McpPromptSpecification> prompts)
prompts - List of prompt specifications. Must not be null.IllegalArgumentException - if prompts is nullprompts(McpPromptSpecification...)public McpServer prompts(McpPromptSpecification... prompts)
Example usage:
.prompts(
new PromptSpecification(analysisPrompt, analysisHandler),
new PromptSpecification(summaryPrompt, summaryHandler),
new PromptSpecification(reviewPrompt, reviewHandler)
)
prompts - The prompt specifications to add. Must not be null.IllegalArgumentException - if prompts is nullpublic McpServer rootsChangeHandler(BiConsumer<McpServerSession,List<McpRoot>> handler)
handler - The handler to register. Must not be null. The function's first
argument is an McpServerSession upon which the server can interact
with the connected client. The second argument is the list of roots.IllegalArgumentException - if consumer is nullpublic McpServer rootsChangeHandlers(List<BiConsumer<McpServerSession,List<McpRoot>>> handlers)
handlers - The list of handlers to register. Must not be null.IllegalArgumentException - if consumers is nullrootsChangeHandler(BiConsumer)@SafeVarargs public final McpServer rootsChangeHandlers(BiConsumer<McpServerSession,List<McpRoot>>... handlers)
handlers - The handlers to register. Must not be null.IllegalArgumentException - if consumers is nullrootsChangeHandlers(List)public HttpResponse sseEndpoint(HttpRequest request)
request - HttpRequestpublic HttpResponse sseMessageEndpoint(HttpRequest request)
request - HttpRequestpublic void sendHeartbeat()
public String getMessageEndpoint()
public String getSseEndpoint()
Copyright © 2025. All rights reserved.