@todoist/mcp-core 0.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +238 -32
  3. package/dist/annotations.d.ts +15 -0
  4. package/dist/annotations.d.ts.map +1 -0
  5. package/dist/annotations.js +23 -0
  6. package/dist/annotations.js.map +1 -0
  7. package/dist/app-meta.d.ts +8 -0
  8. package/dist/app-meta.d.ts.map +1 -0
  9. package/dist/app-meta.js +21 -0
  10. package/dist/app-meta.js.map +1 -0
  11. package/dist/host/challenge.d.ts +18 -0
  12. package/dist/host/challenge.d.ts.map +1 -0
  13. package/dist/host/challenge.js +10 -0
  14. package/dist/host/challenge.js.map +1 -0
  15. package/dist/host/index.d.ts +6 -0
  16. package/dist/host/index.d.ts.map +1 -0
  17. package/dist/host/index.js +5 -0
  18. package/dist/host/index.js.map +1 -0
  19. package/dist/host/server-contract.d.ts +31 -0
  20. package/dist/host/server-contract.d.ts.map +1 -0
  21. package/dist/host/server-contract.js +2 -0
  22. package/dist/host/server-contract.js.map +1 -0
  23. package/dist/host/transient.d.ts +9 -0
  24. package/dist/host/transient.d.ts.map +1 -0
  25. package/dist/host/transient.js +50 -0
  26. package/dist/host/transient.js.map +1 -0
  27. package/dist/host/validate-bearer-token.d.ts +43 -0
  28. package/dist/host/validate-bearer-token.d.ts.map +1 -0
  29. package/dist/host/validate-bearer-token.js +46 -0
  30. package/dist/host/validate-bearer-token.js.map +1 -0
  31. package/dist/host/verdict-cache.d.ts +21 -0
  32. package/dist/host/verdict-cache.d.ts.map +1 -0
  33. package/dist/host/verdict-cache.js +38 -0
  34. package/dist/host/verdict-cache.js.map +1 -0
  35. package/dist/index.d.ts +8 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +7 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/register-tool.d.ts +41 -0
  40. package/dist/register-tool.d.ts.map +1 -0
  41. package/dist/register-tool.js +44 -0
  42. package/dist/register-tool.js.map +1 -0
  43. package/dist/sanitize.d.ts +18 -0
  44. package/dist/sanitize.d.ts.map +1 -0
  45. package/dist/sanitize.js +44 -0
  46. package/dist/sanitize.js.map +1 -0
  47. package/dist/testing/advertised-schemas.d.ts +25 -0
  48. package/dist/testing/advertised-schemas.d.ts.map +1 -0
  49. package/dist/testing/advertised-schemas.js +68 -0
  50. package/dist/testing/advertised-schemas.js.map +1 -0
  51. package/dist/testing/index.d.ts +5 -0
  52. package/dist/testing/index.d.ts.map +1 -0
  53. package/dist/testing/index.js +5 -0
  54. package/dist/testing/index.js.map +1 -0
  55. package/dist/testing/nullable-output.d.ts +15 -0
  56. package/dist/testing/nullable-output.d.ts.map +1 -0
  57. package/dist/testing/nullable-output.js +57 -0
  58. package/dist/testing/nullable-output.js.map +1 -0
  59. package/dist/testing/token-footprint.d.ts +45 -0
  60. package/dist/testing/token-footprint.d.ts.map +1 -0
  61. package/dist/testing/token-footprint.js +57 -0
  62. package/dist/testing/token-footprint.js.map +1 -0
  63. package/dist/testing/tool-annotations.d.ts +24 -0
  64. package/dist/testing/tool-annotations.d.ts.map +1 -0
  65. package/dist/testing/tool-annotations.js +32 -0
  66. package/dist/testing/tool-annotations.js.map +1 -0
  67. package/dist/tool-output.d.ts +21 -0
  68. package/dist/tool-output.d.ts.map +1 -0
  69. package/dist/tool-output.js +32 -0
  70. package/dist/tool-output.js.map +1 -0
  71. package/dist/tool-schemas.d.ts +15 -0
  72. package/dist/tool-schemas.d.ts.map +1 -0
  73. package/dist/tool-schemas.js +42 -0
  74. package/dist/tool-schemas.js.map +1 -0
  75. package/dist/tool.d.ts +68 -0
  76. package/dist/tool.d.ts.map +1 -0
  77. package/dist/tool.js +2 -0
  78. package/dist/tool.js.map +1 -0
  79. package/package.json +76 -8
@@ -0,0 +1,46 @@
1
+ const DEFAULT_TIMEOUT_MS = 5_000;
2
+ /**
3
+ * The validation call got an answer that is no verdict on the token: a 5xx, a 429, a redirect,
4
+ * or a 404 from a misconfigured base URL. `httpStatusCode` is what a host reads to treat a 5xx
5
+ * as transient.
6
+ */
7
+ export class TokenValidationError extends Error {
8
+ httpStatusCode;
9
+ constructor(httpStatusCode) {
10
+ super(`Token validation failed with status ${httpStatusCode}`);
11
+ this.name = 'TokenValidationError';
12
+ this.httpStatusCode = httpStatusCode;
13
+ }
14
+ }
15
+ /**
16
+ * Validates a bearer token with one status-only `GET`: 2xx is `valid`, 401 `invalid`, 403
17
+ * `forbidden` (or whatever `classifyForbidden` says). The body is drained unread, so a change
18
+ * to the response's shape can never misclassify a token.
19
+ *
20
+ * Redirects are not followed: a 302 to a sign-in page that anyone can load would otherwise
21
+ * come back 200 and pass for an accepted token.
22
+ *
23
+ * @throws {TokenValidationError} for any other status, a redirect included. Network and timeout
24
+ * errors propagate unchanged.
25
+ */
26
+ export async function validateBearerToken({ url, token, headers, timeoutMs = DEFAULT_TIMEOUT_MS, classifyForbidden = () => 'forbidden', fetch: send = fetch, }) {
27
+ const requestHeaders = new Headers(headers);
28
+ requestHeaders.set('Authorization', `Bearer ${token}`);
29
+ const response = await send(url, {
30
+ headers: requestHeaders,
31
+ redirect: 'manual',
32
+ signal: AbortSignal.timeout(timeoutMs),
33
+ });
34
+ // Read to the end without decoding, so the connection goes back to the pool: cancelling the
35
+ // body instead closes it, and the next validation pays for a new handshake. The verdict comes
36
+ // from the status, so a body that fails to arrive does not change it.
37
+ await response.body?.pipeTo(new WritableStream()).catch(() => undefined);
38
+ if (response.ok)
39
+ return 'valid';
40
+ if (response.status === 401)
41
+ return 'invalid';
42
+ if (response.status === 403)
43
+ return classifyForbidden(response);
44
+ throw new TokenValidationError(response.status);
45
+ }
46
+ //# sourceMappingURL=validate-bearer-token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate-bearer-token.js","sourceRoot":"","sources":["../../src/host/validate-bearer-token.ts"],"names":[],"mappings":"AAEA,MAAM,kBAAkB,GAAG,KAAK,CAAA;AAEhC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AAC"}
@@ -0,0 +1,21 @@
1
+ import type { TokenValidationResult } from './server-contract.js';
2
+ export type TokenVerdictCacheOptions = {
3
+ /** How long a verdict stands before the token is validated again. */
4
+ ttlMs: number;
5
+ /** The most verdicts kept; the oldest goes first when full. */
6
+ maxEntries: number;
7
+ /** The clock, for tests. Defaults to `Date.now`. */
8
+ now?: () => number;
9
+ };
10
+ export type TokenVerdictCache = {
11
+ get(token: string, baseUrl?: string): TokenValidationResult | undefined;
12
+ set(token: string, baseUrl: string | undefined, result: TokenValidationResult): void;
13
+ clear(): void;
14
+ };
15
+ /**
16
+ * Remembers token verdicts for a while, so a host doesn't validate the same token on every
17
+ * request. Keyed by token and base URL, so one token is never judged against another
18
+ * environment's verdict. Store only verdicts: a transient failure is not one.
19
+ */
20
+ export declare function createTokenVerdictCache({ ttlMs, maxEntries, now, }: TokenVerdictCacheOptions): TokenVerdictCache;
21
+ //# sourceMappingURL=verdict-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verdict-cache.d.ts","sourceRoot":"","sources":["../../src/host/verdict-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AAEjE,MAAM,MAAM,wBAAwB,GAAG;IACnC,qEAAqE;IACrE,KAAK,EAAE,MAAM,CAAA;IACb,+DAA+D;IAC/D,UAAU,EAAE,MAAM,CAAA;IAClB,oDAAoD;IACpD,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;CACrB,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC5B,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,qBAAqB,GAAG,SAAS,CAAA;IACvE,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,EAAE,qBAAqB,GAAG,IAAI,CAAA;IACpF,KAAK,IAAI,IAAI,CAAA;CAChB,CAAA;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,EACpC,KAAK,EACL,UAAU,EACV,GAAc,GACjB,EAAE,wBAAwB,GAAG,iBAAiB,CA4B9C"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Remembers token verdicts for a while, so a host doesn't validate the same token on every
3
+ * request. Keyed by token and base URL, so one token is never judged against another
4
+ * environment's verdict. Store only verdicts: a transient failure is not one.
5
+ */
6
+ export function createTokenVerdictCache({ ttlMs, maxEntries, now = Date.now, }) {
7
+ const entries = new Map();
8
+ const keyOf = (token, baseUrl) => `${token}::${baseUrl ?? ''}`;
9
+ return {
10
+ get(token, baseUrl) {
11
+ const key = keyOf(token, baseUrl);
12
+ const entry = entries.get(key);
13
+ if (!entry)
14
+ return undefined;
15
+ if (now() < entry.expiresAt)
16
+ return entry.result;
17
+ entries.delete(key);
18
+ return undefined;
19
+ },
20
+ set(token, baseUrl, result) {
21
+ if (maxEntries <= 0)
22
+ return;
23
+ const key = keyOf(token, baseUrl);
24
+ // Re-inserting moves the key to the end, so eviction order stays oldest-first.
25
+ entries.delete(key);
26
+ if (entries.size >= maxEntries) {
27
+ const oldest = entries.keys().next().value;
28
+ if (oldest !== undefined)
29
+ entries.delete(oldest);
30
+ }
31
+ entries.set(key, { result, expiresAt: now() + ttlMs });
32
+ },
33
+ clear() {
34
+ entries.clear();
35
+ },
36
+ };
37
+ }
38
+ //# sourceMappingURL=verdict-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verdict-cache.js","sourceRoot":"","sources":["../../src/host/verdict-cache.ts"],"names":[],"mappings":"AAiBA;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CAAC,EACpC,KAAK,EACL,UAAU,EACV,GAAG,GAAG,IAAI,CAAC,GAAG,GACS;IACvB,MAAM,OAAO,GAAG,IAAI,GAAG,EAAgE,CAAA;IACvF,MAAM,KAAK,GAAG,CAAC,KAAa,EAAE,OAAgB,EAAE,EAAE,CAAC,GAAG,KAAK,KAAK,OAAO,IAAI,EAAE,EAAE,CAAA;IAE/E,OAAO;QACH,GAAG,CAAC,KAAK,EAAE,OAAO;YACd,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;YACjC,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YAC9B,IAAI,CAAC,KAAK;gBAAE,OAAO,SAAS,CAAA;YAC5B,IAAI,GAAG,EAAE,GAAG,KAAK,CAAC,SAAS;gBAAE,OAAO,KAAK,CAAC,MAAM,CAAA;YAChD,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;YACnB,OAAO,SAAS,CAAA;QACpB,CAAC;QACD,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM;YACtB,IAAI,UAAU,IAAI,CAAC;gBAAE,OAAM;YAC3B,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;YACjC,+EAA+E;YAC/E,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;YACnB,IAAI,OAAO,CAAC,IAAI,IAAI,UAAU,EAAE,CAAC;gBAC7B,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAA;gBAC1C,IAAI,MAAM,KAAK,SAAS;oBAAE,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;YACpD,CAAC;YACD,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,KAAK,EAAE,CAAC,CAAA;QAC1D,CAAC;QACD,KAAK;YACD,OAAO,CAAC,KAAK,EAAE,CAAA;QACnB,CAAC;KACJ,CAAA;AACL,CAAC"}
@@ -0,0 +1,8 @@
1
+ export { formatToolTitle, getMcpAnnotations } from './annotations.js';
2
+ export { normalizeToolMeta } from './app-meta.js';
3
+ export { createToolRegistrar, type RegisterTool, type ToolFailure, type ToolRegistrarOptions, } from './register-tool.js';
4
+ export { removeNullFields, type RemoveNullFieldsOptions } from './sanitize.js';
5
+ export type { AnyMcpTool, ExecuteResult, McpTool, RequiredToolAnnotations, ToolResult, ToolSchemaSource, } from './tool.js';
6
+ export { getErrorOutput, getToolOutput, type ToolOutputOptions } from './tool-output.js';
7
+ export { getToolSchemas, type ToolSchemas } from './tool-schemas.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EACH,mBAAmB,EACnB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,oBAAoB,GAC5B,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAC9E,YAAY,EACR,UAAU,EACV,aAAa,EACb,OAAO,EACP,uBAAuB,EACvB,UAAU,EACV,gBAAgB,GACnB,MAAM,WAAW,CAAA;AAClB,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACxF,OAAO,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export { formatToolTitle, getMcpAnnotations } from './annotations.js';
2
+ export { normalizeToolMeta } from './app-meta.js';
3
+ export { createToolRegistrar, } from './register-tool.js';
4
+ export { removeNullFields } from './sanitize.js';
5
+ export { getErrorOutput, getToolOutput } from './tool-output.js';
6
+ export { getToolSchemas } from './tool-schemas.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EACH,mBAAmB,GAItB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,gBAAgB,EAAgC,MAAM,eAAe,CAAA;AAS9E,OAAO,EAAE,cAAc,EAAE,aAAa,EAA0B,MAAM,kBAAkB,CAAA;AACxF,OAAO,EAAE,cAAc,EAAoB,MAAM,mBAAmB,CAAA"}
@@ -0,0 +1,41 @@
1
+ import type { McpServer, ServerContext } from '@modelcontextprotocol/server';
2
+ import type { z } from 'zod';
3
+ import { type ToolOutputOptions } from './tool-output.js';
4
+ import type { AnyMcpTool, ExecuteResult } from './tool.js';
5
+ /** A tool call that threw. */
6
+ export type ToolFailure<Context> = {
7
+ tool: AnyMcpTool<Context>;
8
+ /** As the SDK validated them. Write tools carry user content here, so log with care. */
9
+ args: Record<string, unknown>;
10
+ error: unknown;
11
+ };
12
+ export type ToolRegistrarOptions<Context> = ToolOutputOptions & {
13
+ /** Names the product in every tool title: "Todoist" gives "Todoist: Find Tasks". */
14
+ titlePrefix: string;
15
+ /** Builds what `execute` receives. Called on every run, so on every retry `wrapExecute` makes. */
16
+ createContext: (tool: AnyMcpTool<Context>, serverContext: ServerContext) => Context;
17
+ /** Turns whatever a tool threw into text the model can act on. */
18
+ formatError: (error: unknown) => string;
19
+ /**
20
+ * Runs around every `execute`, for retries, request tagging or rewriting the result. `run`
21
+ * calls the tool; return its result, or a changed copy of it.
22
+ */
23
+ wrapExecute?: (tool: AnyMcpTool<Context>, run: () => ExecuteResult<z.ZodRawShape>) => ExecuteResult<z.ZodRawShape>;
24
+ /**
25
+ * Called when a tool throws, before the error is returned to the model. The default logs the
26
+ * tool name and the error, never the arguments.
27
+ */
28
+ onError?: (failure: ToolFailure<Context>) => void;
29
+ };
30
+ /** Registers one tool on one server. */
31
+ export type RegisterTool<Context> = (server: McpServer, tool: AnyMcpTool<Context>) => void;
32
+ /**
33
+ * Returns a function that registers tools on an MCP server with the shared behaviour: cached
34
+ * JSON schemas, annotations with a product title, MCP Apps `_meta` filled in for both kinds of
35
+ * host, results shaped by `getToolOutput`, and a thrown error returned to the model as an
36
+ * `isError` result instead of failing the request.
37
+ *
38
+ * Build one per server when the context depends on the server's credentials.
39
+ */
40
+ export declare function createToolRegistrar<Context>({ titlePrefix, createContext, formatError, wrapExecute, onError, ...outputOptions }: ToolRegistrarOptions<Context>): RegisterTool<Context>;
41
+ //# sourceMappingURL=register-tool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register-tool.d.ts","sourceRoot":"","sources":["../src/register-tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAER,SAAS,EACT,aAAa,EAEhB,MAAM,8BAA8B,CAAA;AACrC,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAG5B,OAAO,EAAiC,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAExF,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAE1D,8BAA8B;AAC9B,MAAM,MAAM,WAAW,CAAC,OAAO,IAAI;IAC/B,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA;IACzB,wFAAwF;IACxF,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,KAAK,EAAE,OAAO,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,oBAAoB,CAAC,OAAO,IAAI,iBAAiB,GAAG;IAC5D,oFAAoF;IACpF,WAAW,EAAE,MAAM,CAAA;IACnB,kGAAkG;IAClG,aAAa,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,EAAE,aAAa,EAAE,aAAa,KAAK,OAAO,CAAA;IACnF,kEAAkE;IAClE,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAAA;IACvC;;;OAGG;IACH,WAAW,CAAC,EAAE,CACV,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,EACzB,GAAG,EAAE,MAAM,aAAa,CAAC,CAAC,CAAC,WAAW,CAAC,KACtC,aAAa,CAAC,CAAC,CAAC,WAAW,CAAC,CAAA;IACjC;;;OAGG;IACH,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,WAAW,CAAC,OAAO,CAAC,KAAK,IAAI,CAAA;CACpD,CAAA;AAED,wCAAwC;AACxC,MAAM,MAAM,YAAY,CAAC,OAAO,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,KAAK,IAAI,CAAA;AAM1F;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,EACzC,WAAW,EACX,aAAa,EACb,WAAW,EACX,WAAW,EACX,OAAwB,EACxB,GAAG,aAAa,EACnB,EAAE,oBAAoB,CAAC,OAAO,CAAC,GAAG,YAAY,CAAC,OAAO,CAAC,CAuCvD"}
@@ -0,0 +1,44 @@
1
+ import { getMcpAnnotations } from './annotations.js';
2
+ import { normalizeToolMeta } from './app-meta.js';
3
+ import { getErrorOutput, getToolOutput } from './tool-output.js';
4
+ import { getToolSchemas } from './tool-schemas.js';
5
+ function logToolFailure({ tool, error }) {
6
+ console.error(`Error executing tool ${tool.name}:`, error);
7
+ }
8
+ /**
9
+ * Returns a function that registers tools on an MCP server with the shared behaviour: cached
10
+ * JSON schemas, annotations with a product title, MCP Apps `_meta` filled in for both kinds of
11
+ * host, results shaped by `getToolOutput`, and a thrown error returned to the model as an
12
+ * `isError` result instead of failing the request.
13
+ *
14
+ * Build one per server when the context depends on the server's credentials.
15
+ */
16
+ export function createToolRegistrar({ titlePrefix, createContext, formatError, wrapExecute, onError = logToolFailure, ...outputOptions }) {
17
+ return (server, tool) => {
18
+ // `AnyMcpTool` declares `args` as `never` so that tools of every parameter shape can be
19
+ // held in one collection. Widening it back is safe here and only here: the SDK has
20
+ // already validated `args` against this tool's own `inputSchema` before invoking the
21
+ // callback.
22
+ const execute = tool.execute;
23
+ const { input: inputSchema, output: outputSchema } = getToolSchemas(tool);
24
+ const callback = async (args, serverContext) => {
25
+ try {
26
+ const run = () => execute(args, createContext(tool, serverContext));
27
+ const result = await (wrapExecute ? wrapExecute(tool, run) : run());
28
+ return getToolOutput(result, outputOptions);
29
+ }
30
+ catch (error) {
31
+ onError({ tool, args, error });
32
+ return getErrorOutput(formatError(error));
33
+ }
34
+ };
35
+ server.registerTool(tool.name, {
36
+ description: tool.description,
37
+ inputSchema,
38
+ ...(outputSchema ? { outputSchema } : {}),
39
+ annotations: getMcpAnnotations(tool, titlePrefix),
40
+ ...(tool._meta ? { _meta: normalizeToolMeta(tool._meta) } : {}),
41
+ }, callback);
42
+ };
43
+ }
44
+ //# sourceMappingURL=register-tool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register-tool.js","sourceRoot":"","sources":["../src/register-tool.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,aAAa,EAA0B,MAAM,kBAAkB,CAAA;AACxF,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAoClD,SAAS,cAAc,CAAC,EAAE,IAAI,EAAE,KAAK,EAA8C;IAC/E,OAAO,CAAC,KAAK,CAAC,wBAAwB,IAAI,CAAC,IAAI,GAAG,EAAE,KAAK,CAAC,CAAA;AAC9D,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CAAU,EACzC,WAAW,EACX,aAAa,EACb,WAAW,EACX,WAAW,EACX,OAAO,GAAG,cAAc,EACxB,GAAG,aAAa,EACY;IAC5B,OAAO,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE;QACpB,wFAAwF;QACxF,mFAAmF;QACnF,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,0 +1,18 @@
1
+ export type RemoveNullFieldsOptions = {
2
+ /**
3
+ * Keys whose values are passed through as given, because their nulls and empty objects mean
4
+ * something: a stored JSON Schema (`const: null`), or a document the model sends back
5
+ * unchanged. A null at the key itself is still removed.
6
+ */
7
+ verbatimKeys?: Iterable<string>;
8
+ };
9
+ /**
10
+ * Removes null fields and empty objects recursively. Empty arrays are kept: "no results" is
11
+ * meaningful, an absent key is not.
12
+ *
13
+ * Every tool result goes through this before it is sent, so output schemas must declare optional
14
+ * fields, never nullable ones: the SDK validates the stripped payload, and a field declared
15
+ * nullable but stripped fails the whole call with "Output validation error".
16
+ */
17
+ export declare function removeNullFields<T>(value: T, options?: RemoveNullFieldsOptions): T;
18
+ //# sourceMappingURL=sanitize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../src/sanitize.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,uBAAuB,GAAG;IAClC;;;;OAIG;IACH,YAAY,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;CAClC,CAAA;AAWD;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,GAAE,uBAA4B,GAAG,CAAC,CAGtF"}
@@ -0,0 +1,44 @@
1
+ function isEmptyPlainObject(value) {
2
+ return (value !== null &&
3
+ typeof value === 'object' &&
4
+ !Array.isArray(value) &&
5
+ Object.keys(value).length === 0);
6
+ }
7
+ /**
8
+ * Removes null fields and empty objects recursively. Empty arrays are kept: "no results" is
9
+ * meaningful, an absent key is not.
10
+ *
11
+ * Every tool result goes through this before it is sent, so output schemas must declare optional
12
+ * fields, never nullable ones: the SDK validates the stripped payload, and a field declared
13
+ * nullable but stripped fails the whole call with "Output validation error".
14
+ */
15
+ export function removeNullFields(value, options = {}) {
16
+ const verbatimKeys = new Set(options.verbatimKeys);
17
+ return strip(value, verbatimKeys);
18
+ }
19
+ function strip(value, verbatimKeys) {
20
+ if (value === null || value === undefined) {
21
+ return value;
22
+ }
23
+ if (Array.isArray(value)) {
24
+ return value.map((item) => strip(item, verbatimKeys));
25
+ }
26
+ if (typeof value === 'object') {
27
+ const sanitized = {};
28
+ for (const [key, entry] of Object.entries(value)) {
29
+ if (entry === null)
30
+ continue;
31
+ if (verbatimKeys.has(key)) {
32
+ sanitized[key] = entry;
33
+ continue;
34
+ }
35
+ const cleaned = strip(entry, verbatimKeys);
36
+ if (isEmptyPlainObject(cleaned))
37
+ continue;
38
+ sanitized[key] = cleaned;
39
+ }
40
+ return sanitized;
41
+ }
42
+ return value;
43
+ }
44
+ //# sourceMappingURL=sanitize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitize.js","sourceRoot":"","sources":["../src/sanitize.ts"],"names":[],"mappings":"AASA,SAAS,kBAAkB,CAAC,KAAc;IACtC,OAAO,CACH,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAAK,QAAQ;QACzB,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACrB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,CAClC,CAAA;AACL,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAI,KAAQ,EAAE,OAAO,GAA4B,EAAE;IAC/E,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,CAAA;IAClD,OAAO,KAAK,CAAC,KAAK,EAAE,YAAY,CAAC,CAAA;AACrC,CAAC;AAED,SAAS,KAAK,CAAI,KAAQ,EAAE,YAAiC;IACzD,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxC,OAAO,KAAK,CAAA;IAChB,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,YAAY,CAAC,CAAM,CAAA;IAC9D,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC5B,MAAM,SAAS,GAA4B,EAAE,CAAA;QAC7C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YAC/C,IAAI,KAAK,KAAK,IAAI;gBAAE,SAAQ;YAC5B,IAAI,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;gBACxB,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;gBACtB,SAAQ;YACZ,CAAC;YACD,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,EAAE,YAAY,CAAC,CAAA;YAC1C,IAAI,kBAAkB,CAAC,OAAO,CAAC;gBAAE,SAAQ;YACzC,SAAS,CAAC,GAAG,CAAC,GAAG,OAAO,CAAA;QAC5B,CAAC;QACD,OAAO,SAAc,CAAA;IACzB,CAAC;IAED,OAAO,KAAK,CAAA;AAChB,CAAC"}
@@ -0,0 +1,25 @@
1
+ import type { McpServer, Tool } from '@modelcontextprotocol/server';
2
+ import type { ToolSchemaSource } from '../tool.js';
3
+ /**
4
+ * What a client sees of a server before it calls anything: the `tools/list` payload and the
5
+ * `instructions` block, read through an in-memory client. Closes the server afterwards.
6
+ */
7
+ export declare function readAdvertisedSurface(server: McpServer): Promise<{
8
+ tools: Tool[];
9
+ instructions: string | undefined;
10
+ }>;
11
+ /** The tools a server advertises on `tools/list`, read through an in-memory client. */
12
+ export declare function listAdvertisedTools(server: McpServer): Promise<Tool[]>;
13
+ export type AdvertisedSchemasConfig = {
14
+ /** Every tool the server should register. */
15
+ tools: readonly ToolSchemaSource[];
16
+ /** Builds a fresh server with those tools registered. Called once per test. */
17
+ createServer: () => McpServer;
18
+ };
19
+ /**
20
+ * Tests that a server advertises exactly `tools`, each with JSON Schema 2020-12 schemas that
21
+ * match a direct Zod conversion of the tool's own shapes. A tool registered twice, left out,
22
+ * or advertised with a schema the cache altered fails here.
23
+ */
24
+ export declare function describeAdvertisedToolSchemas({ tools, createServer, }: AdvertisedSchemasConfig): void;
25
+ //# sourceMappingURL=advertised-schemas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"advertised-schemas.d.ts","sourceRoot":"","sources":["../../src/testing/advertised-schemas.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAA;AAGnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAIlD;;;GAGG;AACH,wBAAsB,qBAAqB,CACvC,MAAM,EAAE,SAAS,GAClB,OAAO,CAAC;IAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC,CAa9D;AAED,uFAAuF;AACvF,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAE5E;AAED,MAAM,MAAM,uBAAuB,GAAG;IAClC,6CAA6C;IAC7C,KAAK,EAAE,SAAS,gBAAgB,EAAE,CAAA;IAClC,+EAA+E;IAC/E,YAAY,EAAE,MAAM,SAAS,CAAA;CAChC,CAAA;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,EAC1C,KAAK,EACL,YAAY,GACf,EAAE,uBAAuB,GAAG,IAAI,CAiDhC"}
@@ -0,0 +1,68 @@
1
+ import { Client, InMemoryTransport } from '@modelcontextprotocol/client';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { z } from 'zod';
4
+ const JSON_SCHEMA_2020_12 = 'https://json-schema.org/draft/2020-12/schema';
5
+ /**
6
+ * What a client sees of a server before it calls anything: the `tools/list` payload and the
7
+ * `instructions` block, read through an in-memory client. Closes the server afterwards.
8
+ */
9
+ export async function readAdvertisedSurface(server) {
10
+ const client = new Client({ name: 'mcp-core-testing', version: '1.0.0' });
11
+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
12
+ await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]);
13
+ try {
14
+ const { tools } = await client.listTools();
15
+ return { tools, instructions: client.getInstructions() };
16
+ }
17
+ finally {
18
+ await client.close();
19
+ await server.close();
20
+ }
21
+ }
22
+ /** The tools a server advertises on `tools/list`, read through an in-memory client. */
23
+ export async function listAdvertisedTools(server) {
24
+ return (await readAdvertisedSurface(server)).tools;
25
+ }
26
+ /**
27
+ * Tests that a server advertises exactly `tools`, each with JSON Schema 2020-12 schemas that
28
+ * match a direct Zod conversion of the tool's own shapes. A tool registered twice, left out,
29
+ * or advertised with a schema the cache altered fails here.
30
+ */
31
+ export function describeAdvertisedToolSchemas({ tools, createServer, }) {
32
+ describe('advertised tool schemas', () => {
33
+ it('advertises every tool exactly once', async () => {
34
+ const advertised = await listAdvertisedTools(createServer());
35
+ expect(advertised.map((tool) => tool.name).sort()).toEqual(tools.map((tool) => tool.name).sort());
36
+ });
37
+ it('uses JSON Schema 2020-12 for every tool schema', async () => {
38
+ const advertised = await listAdvertisedTools(createServer());
39
+ for (const tool of advertised) {
40
+ expect(tool.inputSchema.$schema, tool.name).toBe(JSON_SCHEMA_2020_12);
41
+ if (tool.outputSchema) {
42
+ expect(tool.outputSchema.$schema, tool.name).toBe(JSON_SCHEMA_2020_12);
43
+ }
44
+ }
45
+ });
46
+ it('matches a direct Zod conversion for every tool', async () => {
47
+ const advertised = new Map((await listAdvertisedTools(createServer())).map((tool) => [tool.name, tool]));
48
+ for (const tool of tools) {
49
+ const listed = advertised.get(tool.name);
50
+ expect(listed, `missing advertised tool ${tool.name}`).toBeDefined();
51
+ expect(listed?.inputSchema, tool.name).toEqual(z.toJSONSchema(z.object(tool.parameters), {
52
+ target: 'draft-2020-12',
53
+ io: 'input',
54
+ }));
55
+ if (tool.outputSchema) {
56
+ expect(listed?.outputSchema, tool.name).toEqual(z.toJSONSchema(z.object(tool.outputSchema), {
57
+ target: 'draft-2020-12',
58
+ io: 'output',
59
+ }));
60
+ }
61
+ else {
62
+ expect(listed?.outputSchema, tool.name).toBeUndefined();
63
+ }
64
+ }
65
+ });
66
+ });
67
+ }
68
+ //# sourceMappingURL=advertised-schemas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"advertised-schemas.js","sourceRoot":"","sources":["../../src/testing/advertised-schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAA;AAExE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAGvB,MAAM,mBAAmB,GAAG,8CAA8C,CAAA;AAE1E;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACvC,MAAiB;IAEjB,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,IAAI,EAAE,kBAAkB,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAA;IACzE,MAAM,CAAC,eAAe,EAAE,eAAe,CAAC,GAAG,iBAAiB,CAAC,gBAAgB,EAAE,CAAA;IAE/E,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAA;IAErF,IAAI,CAAC;QACD,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,SAAS,EAAE,CAAA;QAC1C,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,CAAC,eAAe,EAAE,EAAE,CAAA;IAC5D,CAAC;YAAS,CAAC;QACP,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;QACpB,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;IACxB,CAAC;AACL,CAAC;AAED,uFAAuF;A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,CACtB,CAAC,MAAM,mBAAmB,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAC/E,CAAA;YAED,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;gBACvB,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;gBACxC,MAAM,CAAC,MAAM,EAAE,2BAA2B,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAA;gBACpE,MAAM,CAAC,MAAM,EAAE,WAAW,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAC1C,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE;oBACtC,MAAM,EAAE,eAAe;oBACvB,EAAE,EAAE,OAAO;iBACd,CAAC,CACL,CAAA;gBAED,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;oBACpB,MAAM,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAC3C,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE;wBACxC,MAAM,EAAE,eAAe;wBACvB,EAAE,EAAE,QAAQ;qBACf,CAAC,CACL,CAAA;gBACL,CAAC;qBAAM,CAAC;oBACJ,MAAM,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,aAAa,EAAE,CAAA;gBAC3D,CAAC;YACL,CAAC;QACL,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;AACN,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { describeAdvertisedToolSchemas, listAdvertisedTools, type AdvertisedSchemasConfig, } from './advertised-schemas.js';
2
+ export { describeOutputSchemaNullability, findNullablePaths } from './nullable-output.js';
3
+ export { describeTokenFootprint, measureTokenFootprint, type CountTokens, type TokenFootprint, type TokenFootprintConfig, type ToolTokenCost, } from './token-footprint.js';
4
+ export { describeToolAnnotations, type ExpectedToolAnnotations, type ToolAnnotationsConfig, } from './tool-annotations.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,6BAA6B,EAC7B,mBAAmB,EACnB,KAAK,uBAAuB,GAC/B,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,+BAA+B,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AACzF,OAAO,EACH,sBAAsB,EACtB,qBAAqB,EACrB,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,oBAAoB,EACzB,KAAK,aAAa,GACrB,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EACH,uBAAuB,EACvB,KAAK,uBAAuB,EAC5B,KAAK,qBAAqB,GAC7B,MAAM,uBAAuB,CAAA"}
@@ -0,0 +1,5 @@
1
+ export { describeAdvertisedToolSchemas, listAdvertisedTools, } from './advertised-schemas.js';
2
+ export { describeOutputSchemaNullability, findNullablePaths } from './nullable-output.js';
3
+ export { describeTokenFootprint, measureTokenFootprint, } from './token-footprint.js';
4
+ export { describeToolAnnotations, } from './tool-annotations.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,6BAA6B,EAC7B,mBAAmB,GAEtB,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,+BAA+B,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AACzF,OAAO,EACH,sBAAsB,EACtB,qBAAqB,GAKxB,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EACH,uBAAuB,GAG1B,MAAM,uBAAuB,CAAA"}
@@ -0,0 +1,15 @@
1
+ import type { ToolSchemaSource } from '../tool.js';
2
+ /**
3
+ * Every path in a JSON Schema that admits `null`, as dotted names (`items[]` for array items,
4
+ * `$defs.Name` for definitions). An empty list means nothing in the schema can be null.
5
+ */
6
+ export declare function findNullablePaths(node: unknown, path?: string): string[];
7
+ /**
8
+ * One test per tool asserting its output schema admits no `null` anywhere.
9
+ *
10
+ * Results are stripped of nulls before the SDK validates them against the output schema, so a
11
+ * nullable field promises a value the tool can never send, and fails the call with "Output
12
+ * validation error" once stripped. Declare the field `.optional()` and leave the key out instead.
13
+ */
14
+ export declare function describeOutputSchemaNullability(tools: readonly ToolSchemaSource[]): void;
15
+ //# sourceMappingURL=nullable-output.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nullable-output.d.ts","sourceRoot":"","sources":["../../src/testing/nullable-output.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAElD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,SAAK,GAAG,MAAM,EAAE,CAyCpE;AAED;;;;;;GAMG;AACH,wBAAgB,+BAA+B,CAAC,KAAK,EAAE,SAAS,gBAAgB,EAAE,GAAG,IAAI,CAWxF"}
@@ -0,0 +1,57 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { z } from 'zod';
3
+ /**
4
+ * Every path in a JSON Schema that admits `null`, as dotted names (`items[]` for array items,
5
+ * `$defs.Name` for definitions). An empty list means nothing in the schema can be null.
6
+ */
7
+ export function findNullablePaths(node, path = '') {
8
+ if (!node || typeof node !== 'object') {
9
+ return [];
10
+ }
11
+ if (Array.isArray(node)) {
12
+ return node.flatMap((item) => findNullablePaths(item, path));
13
+ }
14
+ const schema = node;
15
+ const { type } = schema;
16
+ if (type === 'null' || (Array.isArray(type) && type.includes('null'))) {
17
+ return [path];
18
+ }
19
+ const paths = [];
20
+ const properties = schema.properties;
21
+ if (properties && typeof properties === 'object') {
22
+ for (const [key, value] of Object.entries(properties)) {
23
+ paths.push(...findNullablePaths(value, path ? `${path}.${key}` : key));
24
+ }
25
+ }
26
+ for (const key of ['items', 'additionalProperties', 'not']) {
27
+ paths.push(...findNullablePaths(schema[key], key === 'items' ? `${path}[]` : path));
28
+ }
29
+ for (const key of ['anyOf', 'oneOf', 'allOf', 'prefixItems']) {
30
+ paths.push(...findNullablePaths(schema[key], path));
31
+ }
32
+ const defs = schema.$defs;
33
+ if (defs && typeof defs === 'object') {
34
+ for (const [key, value] of Object.entries(defs)) {
35
+ paths.push(...findNullablePaths(value, `$defs.${key}`));
36
+ }
37
+ }
38
+ return paths;
39
+ }
40
+ /**
41
+ * One test per tool asserting its output schema admits no `null` anywhere.
42
+ *
43
+ * Results are stripped of nulls before the SDK validates them against the output schema, so a
44
+ * nullable field promises a value the tool can never send, and fails the call with "Output
45
+ * validation error" once stripped. Declare the field `.optional()` and leave the key out instead.
46
+ */
47
+ export function describeOutputSchemaNullability(tools) {
48
+ describe('tool output schemas', () => {
49
+ it.each(tools.filter((tool) => tool.outputSchema).map((tool) => [tool.name, tool]))('%s declares no nullable output fields', (_name, tool) => {
50
+ const jsonSchema = z.toJSONSchema(z.object(tool.outputSchema ?? {}), {
51
+ io: 'output',
52
+ });
53
+ expect(findNullablePaths(jsonSchema)).toEqual([]);
54
+ });
55
+ });
56
+ }
57
+ //# sourceMappingURL=nullable-output.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nullable-output.js","sourceRoot":"","sources":["../../src/testing/nullable-output.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAGvB;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAa,EAAE,IAAI,GAAG,EAAE;IACtD,IAAI,CAAC,IAA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A;IACN,CAAC,CAAC,CAAA;AACN,CAAC"}
@@ -0,0 +1,45 @@
1
+ import type { McpServer } from '@modelcontextprotocol/server';
2
+ /** Counts tokens the way the model a server targets would. */
3
+ export type CountTokens = (text: string) => number;
4
+ /** What one advertised tool costs, as listed on `tools/list`. */
5
+ export type ToolTokenCost = {
6
+ name: string;
7
+ /** The whole tool entry: name, title, description, schemas, annotations, `_meta`. */
8
+ total: number;
9
+ description: number;
10
+ inputSchema: number;
11
+ outputSchema: number;
12
+ };
13
+ /** The fixed cost every session pays before a tool runs. */
14
+ export type TokenFootprint = {
15
+ /** Per tool, most expensive first. A breakdown: these don't add up to `toolsList` exactly. */
16
+ tools: ToolTokenCost[];
17
+ /** The `tools/list` payload, measured as the one array a client receives. */
18
+ toolsList: number;
19
+ /** The `instructions` block, or 0 when the server sends none. */
20
+ instructions: number;
21
+ combined: number;
22
+ };
23
+ /**
24
+ * Measures the fixed token cost of a server from what it advertises to a client, so nothing
25
+ * here restates how registration titles a tool or compiles its schemas. Closes the server.
26
+ */
27
+ export declare function measureTokenFootprint(server: McpServer, countTokens: CountTokens): Promise<TokenFootprint>;
28
+ export type TokenFootprintConfig = {
29
+ /** Builds a fresh server with every tool registered. */
30
+ createServer: () => McpServer;
31
+ /** The tokenizer, such as `(text) => encode(text).length` from `gpt-tokenizer`. */
32
+ countTokens: CountTokens;
33
+ /**
34
+ * What the fixed cost must stay under. Treat it as a ratchet rather than headroom: a rise
35
+ * needs justifying in the change that causes it.
36
+ */
37
+ budget: number;
38
+ };
39
+ /**
40
+ * Tests that the `tools/list` payload plus the `instructions` block stay under `budget`, and
41
+ * prints the cost per tool so a reviewer can see where it went. Fails as well if either half
42
+ * goes missing: a lost block would otherwise read as a saving.
43
+ */
44
+ export declare function describeTokenFootprint({ createServer, countTokens, budget, }: TokenFootprintConfig): void;
45
+ //# sourceMappingURL=token-footprint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-footprint.d.ts","sourceRoot":"","sources":["../../src/testing/token-footprint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAI7D,8DAA8D;AAC9D,MAAM,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAA;AAElD,iEAAiE;AACjE,MAAM,MAAM,aAAa,GAAG;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,qFAAqF;IACrF,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,EAAE,MAAM,CAAA;IACnB,WAAW,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE,MAAM,CAAA;CACvB,CAAA;AAED,4DAA4D;AAC5D,MAAM,MAAM,cAAc,GAAG;IACzB,8FAA8F;IAC9F,KAAK,EAAE,aAAa,EAAE,CAAA;IACtB,6EAA6E;IAC7E,SAAS,EAAE,MAAM,CAAA;IACjB,iEAAiE;IACjE,YAAY,EAAE,MAAM,CAAA;IACpB,QAAQ,EAAE,MAAM,CAAA;CACnB,CAAA;AAED;;;GAGG;AACH,wBAAsB,qBAAqB,CACvC,MAAM,EAAE,SAAS,EACjB,WAAW,EAAE,WAAW,GACzB,OAAO,CAAC,cAAc,CAAC,CAqBzB;AAoBD,MAAM,MAAM,oBAAoB,GAAG;IAC/B,wDAAwD;IACxD,YAAY,EAAE,MAAM,SAAS,CAAA;IAC7B,mFAAmF;IACnF,WAAW,EAAE,WAAW,CAAA;IACxB;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAA;CACjB,CAAA;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,EACnC,YAAY,EACZ,WAAW,EACX,MAAM,GACT,EAAE,oBAAoB,GAAG,IAAI,CAW7B"}