feat: allow overriding protocol versions in Client, Server, and Transport (#1448)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
92f5ce59ba
commit
160902eb2e
1 changed files with 119 additions and 9 deletions
66
examples/server/src/customProtocolVersion.ts
Normal file
66
examples/server/src/customProtocolVersion.ts
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
/**
|
||||
* Example: Custom Protocol Version Support
|
||||
*
|
||||
* This demonstrates how to support protocol versions not yet in the SDK.
|
||||
* First version in the list is used as fallback when client requests
|
||||
* an unsupported version.
|
||||
*
|
||||
* Run with: pnpm tsx src/customProtocolVersion.ts
|
||||
*/
|
||||
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { createServer } from 'node:http';
|
||||
|
||||
import { NodeStreamableHTTPServerTransport } from '@modelcontextprotocol/node';
|
||||
import type { CallToolResult } from '@modelcontextprotocol/server';
|
||||
import { McpServer, SUPPORTED_PROTOCOL_VERSIONS } from '@modelcontextprotocol/server';
|
||||
|
||||
// Add support for a newer protocol version (first in list is fallback)
|
||||
const CUSTOM_VERSIONS = ['2026-01-01', ...SUPPORTED_PROTOCOL_VERSIONS];
|
||||
|
||||
const server = new McpServer(
|
||||
{ name: 'custom-protocol-server', version: '1.0.0' },
|
||||
{
|
||||
supportedProtocolVersions: CUSTOM_VERSIONS,
|
||||
capabilities: { tools: {} }
|
||||
}
|
||||
);
|
||||
|
||||
// Register a tool that shows the protocol configuration
|
||||
server.registerTool(
|
||||
'get-protocol-info',
|
||||
{
|
||||
title: 'Protocol Info',
|
||||
description: 'Returns protocol version configuration',
|
||||
inputSchema: {}
|
||||
},
|
||||
async (): Promise<CallToolResult> => ({
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: JSON.stringify({ supportedVersions: CUSTOM_VERSIONS }, null, 2)
|
||||
}
|
||||
]
|
||||
})
|
||||
);
|
||||
|
||||
// Create transport - server passes versions automatically during connect()
|
||||
const transport = new NodeStreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID()
|
||||
});
|
||||
|
||||
await server.connect(transport);
|
||||
|
||||
// Simple HTTP server
|
||||
const PORT = process.env.MCP_PORT ? Number.parseInt(process.env.MCP_PORT, 10) : 3000;
|
||||
|
||||
createServer(async (req, res) => {
|
||||
if (req.url === '/mcp') {
|
||||
await transport.handleRequest(req, res);
|
||||
} else {
|
||||
res.writeHead(404).end('Not Found');
|
||||
}
|
||||
}).listen(PORT, () => {
|
||||
console.log(`MCP server with custom protocol versions on port ${PORT}`);
|
||||
console.log(`Supported versions: ${CUSTOM_VERSIONS.join(', ')}`);
|
||||
});
|
||||
Loading…
Reference in a new issue