fix(server,client): non-SEP draft spec conformance (eager list handlers, pagination docs, path-sanitization note) (#2269)

Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com>
This commit is contained in:
Matt 2026-06-15 13:25:35 +01:00 • committed by GitHub
commit e84c3e9ad0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 201 additions and 6 deletions

View file

@ -70,6 +70,21 @@ export class McpServer {
constructor(serverInfo: Implementation, options?: ServerOptions) {
this.server = new Server(serverInfo, options);
// Per the MCP spec, a server that declares a primitive capability MUST respond to its
// list method (potentially with an empty result) rather than "Method not found" — even
// if nothing has been registered yet. Handlers are normally installed lazily on first
// registration, so eagerly install them here for any capability declared up front.
// (Users of the low-level `Server` class remain responsible for their own handlers.)
if (options?.capabilities?.tools) {
this.setToolRequestHandlers();
}
if (options?.capabilities?.resources) {
this.setResourceRequestHandlers();
}
if (options?.capabilities?.prompts) {
this.setPromptRequestHandlers();
}
}
/**
@ -111,6 +126,9 @@ export class McpServer {
}
});
// Note: tools are listed in registration (insertion) order, which keeps the ordering
// deterministic across requests when the underlying tool set has not changed, as
// recommended by the spec.
this.server.setRequestHandler(
'tools/list',
(): ListToolsResult => ({

View file

@ -54,6 +54,13 @@ import { DefaultJsonSchemaValidator } from '@modelcontextprotocol/server/_shims'
export type ServerOptions = ProtocolOptions & {
/**
* Capabilities to advertise as being supported by this server.
*
* Note: per the MCP spec, a server that declares a capability MUST respond to that
* capability's requests (e.g. `tools/list` for `tools`) — potentially with an empty
* result — rather than with a "Method not found" error. {@linkcode server/mcp.McpServer | McpServer}
* handles this automatically for capabilities declared here; when using the low-level
* {@linkcode Server} directly, you are responsible for registering a request handler for
* every capability you declare.
*/
capabilities?: ServerCapabilities;