|
1 | 1 | import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
2 | | -import { McpError, ErrorCode, SetLevelRequestSchema, LoggingLevelSchema, type LoggingLevel } from '@modelcontextprotocol/sdk/types.js'; |
| 2 | +import { McpError, ErrorCode, SetLevelRequestSchema, ListToolsRequestSchema, LoggingLevelSchema, type LoggingLevel } from '@modelcontextprotocol/sdk/types.js'; |
3 | 3 | import { z } from 'zod'; |
4 | 4 | import { randomBytes, randomInt } from 'node:crypto'; |
5 | 5 | import { UptimeKumaClient } from './uptime-kuma-client.js'; |
@@ -146,6 +146,49 @@ export async function createServer(config: UptimeKumaConfig): Promise<{ server: |
146 | 146 | return schema; |
147 | 147 | } |
148 | 148 |
|
| 149 | + // Issue #83: the SDK converts every tool's input/output Zod schema to JSON Schema via |
| 150 | + // zod-to-json-schema without setting a `target`, so each one is stamped |
| 151 | + // "$schema": "http://json-schema.org/draft-07/schema#" — verified against both the SDK |
| 152 | + // version this project pins (^1.22.0) and the current latest (1.30.0); the behavior is |
| 153 | + // unchanged between them. Clients whose validator only accepts the MCP spec's default |
| 154 | + // dialect (2020-12, per SEP-1613) reject every tool outright before ever calling it |
| 155 | + // (see modelcontextprotocol/typescript-sdk#745, still open upstream). |
| 156 | + // |
| 157 | + // None of the schemas below use anything that differs between the two drafts (no |
| 158 | + // `dependencies`, no positional tuple items), so relabeling the declared dialect is safe — |
| 159 | + // only the version stamp was wrong, not the shape. This intercepts the SDK's own |
| 160 | + // `setRequestHandler(ListToolsRequestSchema, ...)` call (made lazily on first |
| 161 | + // `registerTool`, below) rather than reimplementing tools/list, so it keeps working across |
| 162 | + // SDK internals changing between versions. |
| 163 | + const DRAFT_07_DIALECT = 'http://json-schema.org/draft-07/schema#'; |
| 164 | + const DIALECT_2020_12 = 'https://json-schema.org/draft/2020-12/schema'; |
| 165 | + const relabelDialect = (value: unknown): unknown => { |
| 166 | + if (Array.isArray(value)) return value.map(relabelDialect); |
| 167 | + if (value && typeof value === 'object') { |
| 168 | + const out: Record<string, unknown> = {}; |
| 169 | + for (const [key, val] of Object.entries(value as Record<string, unknown>)) { |
| 170 | + out[key] = key === '$schema' && val === DRAFT_07_DIALECT ? DIALECT_2020_12 : relabelDialect(val); |
| 171 | + } |
| 172 | + return out; |
| 173 | + } |
| 174 | + return value; |
| 175 | + }; |
| 176 | + |
| 177 | + const setRequestHandlerUnpatched = server.server.setRequestHandler.bind(server.server) as ( |
| 178 | + requestSchema: unknown, |
| 179 | + handler: (...args: unknown[]) => unknown |
| 180 | + ) => unknown; |
| 181 | + (server.server as unknown as { setRequestHandler: typeof server.server.setRequestHandler }).setRequestHandler = (( |
| 182 | + requestSchema: unknown, |
| 183 | + handler: (...args: unknown[]) => unknown |
| 184 | + ) => { |
| 185 | + if (requestSchema === ListToolsRequestSchema) { |
| 186 | + const wrapped = async (...args: unknown[]) => relabelDialect(await handler(...args)); |
| 187 | + return setRequestHandlerUnpatched(requestSchema, wrapped); |
| 188 | + } |
| 189 | + return setRequestHandlerUnpatched(requestSchema, handler); |
| 190 | + }) as typeof server.server.setRequestHandler; |
| 191 | + |
149 | 192 | // Handle logging level changes via the underlying server |
150 | 193 | server.server.setRequestHandler(SetLevelRequestSchema, async (request) => { |
151 | 194 | const level = request.params?.level as LoggingLevel | undefined; |
|
0 commit comments