@diffra-ai/mcp 0.0.0-stage → 0.1.1

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 (125) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE +22 -0
  3. package/README.md +190 -2
  4. package/THIRD_PARTY_NOTICES/modelcontextprotocol-typescript-sdk.txt +21 -0
  5. package/dist/auth-provider.d.ts +19 -0
  6. package/dist/auth-provider.d.ts.map +1 -0
  7. package/dist/auth-provider.js +2 -0
  8. package/dist/auth-provider.js.map +1 -0
  9. package/dist/client.d.ts +89 -0
  10. package/dist/client.d.ts.map +1 -0
  11. package/dist/client.js +461 -0
  12. package/dist/client.js.map +1 -0
  13. package/dist/config.d.ts +117 -0
  14. package/dist/config.d.ts.map +1 -0
  15. package/dist/config.js +153 -0
  16. package/dist/config.js.map +1 -0
  17. package/dist/index.d.ts +16 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +14 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/input-schema.d.ts +7 -0
  22. package/dist/input-schema.d.ts.map +1 -0
  23. package/dist/input-schema.js +13 -0
  24. package/dist/input-schema.js.map +1 -0
  25. package/dist/model-content.d.ts +19 -0
  26. package/dist/model-content.d.ts.map +1 -0
  27. package/dist/model-content.js +67 -0
  28. package/dist/model-content.js.map +1 -0
  29. package/dist/naming.d.ts +23 -0
  30. package/dist/naming.d.ts.map +1 -0
  31. package/dist/naming.js +37 -0
  32. package/dist/naming.js.map +1 -0
  33. package/dist/oauth/auth-provider.d.ts +52 -0
  34. package/dist/oauth/auth-provider.d.ts.map +1 -0
  35. package/dist/oauth/auth-provider.js +97 -0
  36. package/dist/oauth/auth-provider.js.map +1 -0
  37. package/dist/oauth/callback.d.ts +48 -0
  38. package/dist/oauth/callback.d.ts.map +1 -0
  39. package/dist/oauth/callback.js +119 -0
  40. package/dist/oauth/callback.js.map +1 -0
  41. package/dist/oauth/client-settings.d.ts +107 -0
  42. package/dist/oauth/client-settings.d.ts.map +1 -0
  43. package/dist/oauth/client-settings.js +114 -0
  44. package/dist/oauth/client-settings.js.map +1 -0
  45. package/dist/oauth/discovery.d.ts +30 -0
  46. package/dist/oauth/discovery.d.ts.map +1 -0
  47. package/dist/oauth/discovery.js +156 -0
  48. package/dist/oauth/discovery.js.map +1 -0
  49. package/dist/oauth/errors.d.ts +32 -0
  50. package/dist/oauth/errors.d.ts.map +1 -0
  51. package/dist/oauth/errors.js +58 -0
  52. package/dist/oauth/errors.js.map +1 -0
  53. package/dist/oauth/flow.d.ts +112 -0
  54. package/dist/oauth/flow.d.ts.map +1 -0
  55. package/dist/oauth/flow.js +360 -0
  56. package/dist/oauth/flow.js.map +1 -0
  57. package/dist/oauth/index.d.ts +11 -0
  58. package/dist/oauth/index.d.ts.map +1 -0
  59. package/dist/oauth/index.js +9 -0
  60. package/dist/oauth/index.js.map +1 -0
  61. package/dist/oauth/loopback.d.ts +51 -0
  62. package/dist/oauth/loopback.d.ts.map +1 -0
  63. package/dist/oauth/loopback.js +98 -0
  64. package/dist/oauth/loopback.js.map +1 -0
  65. package/dist/oauth/provider.d.ts +62 -0
  66. package/dist/oauth/provider.d.ts.map +1 -0
  67. package/dist/oauth/provider.js +118 -0
  68. package/dist/oauth/provider.js.map +1 -0
  69. package/dist/oauth/sign-in.d.ts +73 -0
  70. package/dist/oauth/sign-in.d.ts.map +1 -0
  71. package/dist/oauth/sign-in.js +182 -0
  72. package/dist/oauth/sign-in.js.map +1 -0
  73. package/dist/oauth/types.d.ts +83 -0
  74. package/dist/oauth/types.d.ts.map +1 -0
  75. package/dist/oauth/types.js +113 -0
  76. package/dist/oauth/types.js.map +1 -0
  77. package/dist/output-limit.d.ts +24 -0
  78. package/dist/output-limit.d.ts.map +1 -0
  79. package/dist/output-limit.js +58 -0
  80. package/dist/output-limit.js.map +1 -0
  81. package/dist/protocol/content.d.ts +83 -0
  82. package/dist/protocol/content.d.ts.map +1 -0
  83. package/dist/protocol/content.js +41 -0
  84. package/dist/protocol/content.js.map +1 -0
  85. package/dist/protocol/jsonrpc.d.ts +59 -0
  86. package/dist/protocol/jsonrpc.d.ts.map +1 -0
  87. package/dist/protocol/jsonrpc.js +67 -0
  88. package/dist/protocol/jsonrpc.js.map +1 -0
  89. package/dist/protocol/types.d.ts +123 -0
  90. package/dist/protocol/types.d.ts.map +1 -0
  91. package/dist/protocol/types.js +7 -0
  92. package/dist/protocol/types.js.map +1 -0
  93. package/dist/result-schema.d.ts +10 -0
  94. package/dist/result-schema.d.ts.map +1 -0
  95. package/dist/result-schema.js +78 -0
  96. package/dist/result-schema.js.map +1 -0
  97. package/dist/testing/index.d.ts +2 -0
  98. package/dist/testing/index.d.ts.map +1 -0
  99. package/dist/testing/index.js +2 -0
  100. package/dist/testing/index.js.map +1 -0
  101. package/dist/transports/in-memory.d.ts +19 -0
  102. package/dist/transports/in-memory.d.ts.map +1 -0
  103. package/dist/transports/in-memory.js +50 -0
  104. package/dist/transports/in-memory.js.map +1 -0
  105. package/dist/transports/lines.d.ts +34 -0
  106. package/dist/transports/lines.d.ts.map +1 -0
  107. package/dist/transports/lines.js +85 -0
  108. package/dist/transports/lines.js.map +1 -0
  109. package/dist/transports/pipe.d.ts +49 -0
  110. package/dist/transports/pipe.d.ts.map +1 -0
  111. package/dist/transports/pipe.js +96 -0
  112. package/dist/transports/pipe.js.map +1 -0
  113. package/dist/transports/stdio.d.ts +29 -0
  114. package/dist/transports/stdio.d.ts.map +1 -0
  115. package/dist/transports/stdio.js +170 -0
  116. package/dist/transports/stdio.js.map +1 -0
  117. package/dist/transports/streamable-http.d.ts +103 -0
  118. package/dist/transports/streamable-http.d.ts.map +1 -0
  119. package/dist/transports/streamable-http.js +531 -0
  120. package/dist/transports/streamable-http.js.map +1 -0
  121. package/dist/transports/transport.d.ts +36 -0
  122. package/dist/transports/transport.d.ts.map +1 -0
  123. package/dist/transports/transport.js +38 -0
  124. package/dist/transports/transport.js.map +1 -0
  125. package/package.json +59 -5
@@ -0,0 +1,117 @@
1
+ /**
2
+ * The `mcpServers` configuration shape that MCP clients share, so configurations can be copied between
3
+ * them: stdio servers (`command`, `args`, `env`, `cwd`), Streamable HTTP servers (`url`, `headers`,
4
+ * `oauth`), and `enabled`, `description` and `timeout` for both.
5
+ *
6
+ * ```json
7
+ * {
8
+ * "mcpServers": {
9
+ * "filesystem": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "."] },
10
+ * "docs": { "url": "https://example.com/mcp", "headers": { "Authorization": "Bearer ${DOCS_TOKEN}" } }
11
+ * }
12
+ * }
13
+ * ```
14
+ *
15
+ * Applications add fields of their own: {@link validateMcpServerConfig} keeps fields it does not know
16
+ * for the caller to check afterwards.
17
+ */
18
+ /** Fields every server entry may have. */
19
+ interface McpServerConfigBase {
20
+ /** What the server offers, in a sentence. */
21
+ description?: string;
22
+ /** Set to false to keep the entry without connecting. Default: true. */
23
+ enabled?: boolean;
24
+ /** Per-request timeout in seconds. */
25
+ timeout?: number;
26
+ }
27
+ /** A server started as a local program that speaks MCP over stdin and stdout. */
28
+ export interface McpStdioServerConfig extends McpServerConfigBase {
29
+ type?: "stdio";
30
+ command: string;
31
+ args?: string[];
32
+ /** Added to the environment. The application decides whether values may reference variables (`${NAME}`). */
33
+ env?: Record<string, string>;
34
+ /** The application decides what relative paths resolve against. */
35
+ cwd?: string;
36
+ }
37
+ /** OAuth client settings for servers that do not support dynamic client registration. */
38
+ export interface McpOAuthConfig {
39
+ /** Pre-registered client id. Without it, the application registers a client with the authorization server. */
40
+ clientId?: string;
41
+ /** As configured; the application decides whether it may reference variables. */
42
+ clientSecret?: string;
43
+ /**
44
+ * Port of the loopback callback server, for clients registered with a fixed redirect URI. Without
45
+ * `callbackUrl`, the redirect URI is `http://127.0.0.1:<port>/callback`.
46
+ */
47
+ callbackPort?: number;
48
+ /**
49
+ * Redirect URI registered for `clientId`, for example `http://localhost:8080/oauth/callback`. It must
50
+ * be an `http` URI on `localhost`, `127.0.0.1`, or `[::1]`. Without a port, the callback server
51
+ * listens on `callbackPort` or a free port, which is added to the URI (RFC 8252).
52
+ */
53
+ callbackUrl?: string;
54
+ /** Scopes to request, separated by spaces. Default: the scopes the server advertises. */
55
+ scope?: string;
56
+ /** `client_name` sent with dynamic client registration. Default: the application's name. */
57
+ clientName?: string;
58
+ /**
59
+ * How the application identifies itself without `clientId`. `auto` (default): its Client ID Metadata
60
+ * Document when the authorization server supports them for public clients, otherwise dynamic client
61
+ * registration. `cimd`: always the document, and sign-in fails without support. `dcr`: always
62
+ * dynamic client registration.
63
+ */
64
+ clientRegistration?: "auto" | "dcr" | "cimd";
65
+ /**
66
+ * Authorization server metadata document (RFC 8414 or OpenID Connect discovery) to use instead of
67
+ * discovery through the server, for servers that advertise a wrong authorization server or none.
68
+ * The document is trusted as configured. Must use https, except on loopback hosts.
69
+ */
70
+ authServerMetadataUrl?: string;
71
+ }
72
+ /** A server reached over Streamable HTTP. */
73
+ export interface McpHttpServerConfig extends McpServerConfigBase {
74
+ type?: "http";
75
+ url: string;
76
+ /** Sent with every request. The application decides whether values may reference variables. */
77
+ headers?: Record<string, string>;
78
+ oauth?: McpOAuthConfig;
79
+ }
80
+ /** One entry of the `mcpServers` shape: a stdio or a Streamable HTTP server. */
81
+ export type McpServerConfig = McpStdioServerConfig | McpHttpServerConfig;
82
+ /**
83
+ * A validated server entry, tagged with its transport, or why it is invalid. The config is the entry
84
+ * as given: fields the validator does not know are kept, unchecked, for the caller to check.
85
+ */
86
+ export type McpServerConfigResult = {
87
+ kind: "stdio";
88
+ config: McpStdioServerConfig & Record<string, unknown>;
89
+ } | {
90
+ kind: "http";
91
+ config: McpHttpServerConfig & Record<string, unknown>;
92
+ } | {
93
+ kind: "invalid";
94
+ error: string;
95
+ };
96
+ /** What the application adds to the validation of server entries. */
97
+ export interface McpServerConfigOptions {
98
+ /**
99
+ * Checks the `oauth.callbackUrl` (undefined: none configured) of an entry with
100
+ * `oauth.clientRegistration: "cimd"`: the application's Client ID Metadata Documents must list it.
101
+ * Returns an error message. Without this check, `"cimd"` is refused.
102
+ */
103
+ checkMetadataDocumentCallback?(callbackUrl: string | undefined): string | undefined;
104
+ }
105
+ /**
106
+ * Validate one entry of the `mcpServers` shape (configuration files are a trust boundary). Server
107
+ * names are letters, digits, `_` and `-`, which tool names and namespaces are built from.
108
+ */
109
+ export declare function validateMcpServerConfig(name: string, raw: unknown, options?: McpServerConfigOptions): McpServerConfigResult;
110
+ /**
111
+ * Whether an entry overrides a server defined elsewhere instead of defining one: it has no `command`,
112
+ * `url` or `type`. Applications let a less trusted file (a project's) override a few fields, such as
113
+ * `enabled`, of a server a trusted file defines; which fields is theirs to decide.
114
+ */
115
+ export declare function isMcpServerOverride(entry: Record<string, unknown>): boolean;
116
+ export {};
117
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,0CAA0C;AAC1C,UAAU,mBAAmB;IAC5B,6CAA6C;IAC7C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,wEAAwE;IACxE,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,sCAAsC;IACtC,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,iFAAiF;AACjF,MAAM,WAAW,oBAAqB,SAAQ,mBAAmB;IAChE,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,4GAA4G;IAC5G,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7B,mEAAmE;IACnE,GAAG,CAAC,EAAE,MAAM,CAAC;CACb;AAED,yFAAyF;AACzF,MAAM,WAAW,cAAc;IAC9B,8GAA8G;IAC9G,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iFAAiF;IACjF,YAA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AI,EAAE,MAAM,EACZ,GAAG,EAAE,OAAO,EACZ,OAAO,GAAE,sBAA2B,GAClC,qBAAqB,CAsBvB;AAiGD;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAE3E"}
package/dist/config.js ADDED
@@ -0,0 +1,153 @@
1
+ /**
2
+ * The `mcpServers` configuration shape that MCP clients share, so configurations can be copied between
3
+ * them: stdio servers (`command`, `args`, `env`, `cwd`), Streamable HTTP servers (`url`, `headers`,
4
+ * `oauth`), and `enabled`, `description` and `timeout` for both.
5
+ *
6
+ * ```json
7
+ * {
8
+ * "mcpServers": {
9
+ * "filesystem": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "."] },
10
+ * "docs": { "url": "https://example.com/mcp", "headers": { "Authorization": "Bearer ${DOCS_TOKEN}" } }
11
+ * }
12
+ * }
13
+ * ```
14
+ *
15
+ * Applications add fields of their own: {@link validateMcpServerConfig} keeps fields it does not know
16
+ * for the caller to check afterwards.
17
+ */
18
+ const SERVER_NAME = /^[A-Za-z0-9_-]+$/;
19
+ /**
20
+ * Validate one entry of the `mcpServers` shape (configuration files are a trust boundary). Server
21
+ * names are letters, digits, `_` and `-`, which tool names and namespaces are built from.
22
+ */
23
+ export function validateMcpServerConfig(name, raw, options = {}) {
24
+ const invalid = (error) => ({ kind: "invalid", error });
25
+ if (!SERVER_NAME.test(name))
26
+ return invalid(`invalid server name "${name}" (use letters, digits, "_" and "-")`);
27
+ if (!isRecord(raw))
28
+ return invalid(`server "${name}" must be an object`);
29
+ const fieldsError = validateBaseFields(raw);
30
+ if (fieldsError)
31
+ return invalid(`server "${name}": ${fieldsError}`);
32
+ const { type, url, command } = raw;
33
+ if (type === "sse") {
34
+ return invalid(`server "${name}": legacy SSE transport is not supported; use the streamable HTTP URL`);
35
+ }
36
+ // The casts below hold because the fields were checked one by one, which the types cannot express.
37
+ if (typeof url === "string" && (type === undefined || type === "http" || type === "streamable-http")) {
38
+ const error = validateHttpFields(raw, url, options);
39
+ if (error)
40
+ return invalid(`server "${name}": ${error}`);
41
+ return { kind: "http", config: raw };
42
+ }
43
+ if (typeof command === "string" && (type === undefined || type === "stdio")) {
44
+ const error = validateStdioFields(raw);
45
+ if (error)
46
+ return invalid(`server "${name}": ${error}`);
47
+ return { kind: "stdio", config: raw };
48
+ }
49
+ return invalid(`server "${name}" needs either "command" (stdio) or "url" (streamable HTTP)`);
50
+ }
51
+ function isRecord(value) {
52
+ return typeof value === "object" && value !== null && !Array.isArray(value);
53
+ }
54
+ function validateBaseFields(entry) {
55
+ const { enabled, timeout, description } = entry;
56
+ if (enabled !== undefined && typeof enabled !== "boolean")
57
+ return "enabled must be a boolean";
58
+ if (description !== undefined && typeof description !== "string")
59
+ return "description must be a string";
60
+ if (timeout !== undefined && (typeof timeout !== "number" || !(timeout > 0))) {
61
+ return "timeout must be a positive number of seconds";
62
+ }
63
+ return undefined;
64
+ }
65
+ function validateHttpFields(entry, url, options) {
66
+ if (!URL.canParse(url) || !/^https?:$/.test(new URL(url).protocol))
67
+ return "url must be an http or https URL";
68
+ if (entry.headers !== undefined && !isStringRecord(entry.headers))
69
+ return "headers must map names to strings";
70
+ return validateOAuth(entry.oauth, options);
71
+ }
72
+ function validateStdioFields(entry) {
73
+ const { args, env, cwd } = entry;
74
+ if (args !== undefined && !(Array.isArray(args) && args.every((arg) => typeof arg === "string"))) {
75
+ return "args must be an array of strings";
76
+ }
77
+ if (env !== undefined && !isStringRecord(env))
78
+ return "env must map names to strings";
79
+ if (cwd !== undefined && typeof cwd !== "string")
80
+ return "cwd must be a string";
81
+ return undefined;
82
+ }
83
+ function isStringRecord(value) {
84
+ return isRecord(value) && Object.values(value).every((entry) => typeof entry === "string");
85
+ }
86
+ const LOOPBACK_HOSTS = ["localhost", "127.0.0.1", "[::1]"];
87
+ function validateOAuth(value, options) {
88
+ if (value === undefined)
89
+ return undefined;
90
+ if (!isRecord(value))
91
+ return "oauth must be an object";
92
+ if (value.clientId !== undefined && typeof value.clientId !== "string")
93
+ return "oauth.clientId must be a string";
94
+ if (value.clientSecret !== undefined && typeof value.clientSecret !== "string") {
95
+ return "oauth.clientSecret must be a string";
96
+ }
97
+ const port = value.callbackPort;
98
+ if (port !== undefined && (typeof port !== "number" || !Number.isInteger(port) || port < 1 || port > 65535)) {
99
+ return "oauth.callbackPort must be a port number";
100
+ }
101
+ if (value.callbackUrl !== undefined) {
102
+ if (typeof value.callbackUrl !== "string" || !isLoopbackRedirectUri(value.callbackUrl)) {
103
+ return "oauth.callbackUrl must be an http URI on localhost, 127.0.0.1, or [::1] without query or fragment";
104
+ }
105
+ const urlPort = new URL(value.callbackUrl).port;
106
+ if (urlPort && port !== undefined && Number(urlPort) !== port) {
107
+ return "oauth.callbackUrl and oauth.callbackPort name different ports";
108
+ }
109
+ }
110
+ if (value.scope !== undefined && typeof value.scope !== "string")
111
+ return "oauth.scope must be a string";
112
+ if (value.clientName !== undefined && (typeof value.clientName !== "string" || !value.clientName.trim())) {
113
+ return "oauth.clientName must be a non-empty string";
114
+ }
115
+ const registration = value.clientRegistration;
116
+ if (registration !== undefined && registration !== "auto" && registration !== "dcr") {
117
+ if (registration !== "cimd")
118
+ return 'oauth.clientRegistration must be "auto", "dcr", or "cimd"';
119
+ if (value.clientId !== undefined || value.clientName !== undefined) {
120
+ return 'oauth.clientRegistration "cimd" cannot be combined with oauth.clientId or oauth.clientName';
121
+ }
122
+ if (!options.checkMetadataDocumentCallback) {
123
+ return 'oauth.clientRegistration "cimd" is not supported: there are no Client ID Metadata Documents';
124
+ }
125
+ const callbackError = options.checkMetadataDocumentCallback(typeof value.callbackUrl === "string" ? value.callbackUrl : undefined);
126
+ if (callbackError)
127
+ return callbackError;
128
+ }
129
+ const metadataUrl = value.authServerMetadataUrl;
130
+ if (metadataUrl !== undefined) {
131
+ const url = typeof metadataUrl === "string" && URL.canParse(metadataUrl) ? new URL(metadataUrl) : undefined;
132
+ if (!url || !(url.protocol === "https:" || (url.protocol === "http:" && LOOPBACK_HOSTS.includes(url.hostname)))) {
133
+ return "oauth.authServerMetadataUrl must be an https URL, or http on localhost, 127.0.0.1, or [::1]";
134
+ }
135
+ }
136
+ return undefined;
137
+ }
138
+ /** Whether a redirect URI can be served by a loopback callback server. */
139
+ function isLoopbackRedirectUri(value) {
140
+ if (!URL.canParse(value))
141
+ return false;
142
+ const url = new URL(value);
143
+ return url.protocol === "http:" && LOOPBACK_HOSTS.includes(url.hostname) && url.search === "" && url.hash === "";
144
+ }
145
+ /**
146
+ * Whether an entry overrides a server defined elsewhere instead of defining one: it has no `command`,
147
+ * `url` or `type`. Applications let a less trusted file (a project's) override a few fields, such as
148
+ * `enabled`, of a server a trusted file defines; which fields is theirs to decide.
149
+ */
150
+ export function isMcpServerOverride(entry) {
151
+ return entry.command === undefined && entry.url === undefined && entry.type === undefined;
152
+ }
153
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AA0FH,MAAM,WAAW,GAAG,kBAAkB,CAAC;AAEvC;;;GAGG;AACH,MAAM,UAAU,uBAAuB,CACtC,IAAY,EACZ,GAAY,EACZ,OAAO,GAA2B,EAAE;IAEpC,MAAM,OAAO,GAAG,CAAC,KAAa,EAAyB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;IACvF,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,OAAO,CAAC,wBAAwB,IAAI,sCAAsC,CAAC,CAAC;IAChH,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,OAAO,CAAC,WAAW,IAAI,qBAAqB,CAAC,CAAC;IACzE,MAAM,WAAW,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC;IAC5C,IAAI,WAAW;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MCP clients share, so configurations can be copied between\n * them: stdio servers (`command`, `args`, `env`, `cwd`), Streamable HTTP servers (`url`, `headers`,\n * `oauth`), and `enabled`, `description` and `timeout` for both.\n *\n * ```json\n * {\n * \"mcpServers\": {\n * \"filesystem\": { \"command\": \"npx\", \"args\": [\"-y\", \"@modelcontextprotocol/server-filesystem\", \".\"] },\n * \"docs\": { \"url\": \"https://example.com/mcp\", \"headers\": { \"Authorization\": \"Bearer ${DOCS_TOKEN}\" } }\n * }\n * }\n * ```\n *\n * Applications add fields of their own: {@link validateMcpServerConfig} keeps fields it does not know\n * for the caller to check afterwards.\n */\n\n/** Fields every server entry may have. */\ninterface McpServerConfigBase {\n\t/** What the server offers, in a sentence. */\n\tdescription?: string;\n\t/** Set to false to keep the entry without connecting. Default: true. */\n\tenabled?: boolean;\n\t/** Per-request timeout in seconds. */\n\ttimeout?: number;\n}\n\n/** A server started as a local program that speaks MCP over stdin and stdout. */\nexport interface McpStdioServerConfig extends McpServerConfigBase {\n\ttype?: \"stdio\";\n\tcommand: string;\n\targs?: string[];\n\t/** Added to the environment. The application decides whether values may reference variables (`${NAME}`). */\n\tenv?: Record<string, string>;\n\t/** The application decides what relative paths resolve against. */\n\tcwd?: string;\n}\n\n/** OAuth client settings for servers that do not support dynamic client registration. */\nexport interface McpOAuthConfig {\n\t/** Pre-registered client id. Without it, the application registers a client with the authorization server. */\n\tclientId?: string;\n\t/** As configured; the application decides whether it may reference variables. */\n\tclientSecret?: string;\n\t/**\n\t * Port of the loopback callback server, for clients registered with a fixed redirect URI. Without\n\t * `callbackUrl`, the redirect URI is `http://127.0.0.1:<port>/callback`.\n\t */\n\tcallbackPort?: number;\n\t/**\n\t * Redirect URI registered for `clientId`, for example `http://localhost:8080/oauth/callback`. It must\n\t * be an `http` URI on `localhost`, `127.0.0.1`, or `[::1]`. Without a port, the callback server\n\t * listens on `callbackPort` or a free port, which is added to the URI (RFC 8252).\n\t */\n\tcallbackUrl?: string;\n\t/** Scopes to request, separated by spaces. Default: the scopes the server advertises. */\n\tscope?: string;\n\t/** `client_name` sent with dynamic client registration. Default: the application's name. */\n\tclientName?: string;\n\t/**\n\t * How the application identifies itself without `clientId`. `auto` (default): its Client ID Metadata\n\t * Document when the authorization server supports them for public clients, otherwise dynamic client\n\t * registration. `cimd`: always the document, and sign-in fails without support. `dcr`: always\n\t * dynamic client registration.\n\t */\n\tclientRegistration?: \"auto\" | \"dcr\" | \"cimd\";\n\t/**\n\t * Authorization server metadata document (RFC 8414 or OpenID Connect discovery) to use instead of\n\t * discovery through the server, for servers that advertise a wrong authorization server or none.\n\t * The document is trusted as configured. Must use https, except on loopback hosts.\n\t */\n\tauthServerMetadataUrl?: string;\n}\n\n/** A server reached over Streamable HTTP. */\nexport interface McpHttpServerConfig extends McpServerConfigBase {\n\ttype?: \"http\";\n\turl: string;\n\t/** Sent with every request. The application decides whether values may reference variables. */\n\theaders?: Record<string, string>;\n\toauth?: McpOAuthConfig;\n}\n\n/** One entry of the `mcpServers` shape: a stdio or a Streamable HTTP server. */\nexport type McpServerConfig = McpStdioServerConfig | McpHttpServerConfig;\n\n/**\n * A validated server entry, tagged with its transport, or why it is invalid. The config is the entry\n * as given: fields the validator does not know are kept, unchecked, for the caller to check.\n */\nexport type McpServerConfigResult =\n\t| { kind: \"stdio\"; config: McpStdioServerConfig & Record<string, unknown> }\n\t| { kind: \"http\"; config: McpHttpServerConfig & Record<string, unknown> }\n\t| { kind: \"invalid\"; error: string };\n\n/** What the application adds to the validation of server entries. */\nexport interface McpServerConfigOptions {\n\t/**\n\t * Checks the `oauth.callbackUrl` (undefined: none configured) of an entry with\n\t * `oauth.clientRegistration: \"cimd\"`: the application's Client ID Metadata Documents must list it.\n\t * Returns an error message. Without this check, `\"cimd\"` is refused.\n\t */\n\tcheckMetadataDocumentCallback?(callbackUrl: string | undefined): string | undefined;\n}\n\nconst SERVER_NAME = /^[A-Za-z0-9_-]+$/;\n\n/**\n * Validate one entry of the `mcpServers` shape (configuration files are a trust boundary). Server\n * names are letters, digits, `_` and `-`, which tool names and namespaces are built from.\n */\nexport function validateMcpServerConfig(\n\tname: string,\n\traw: unknown,\n\toptions: McpServerConfigOptions = {},\n): McpServerConfigResult {\n\tconst invalid = (error: string): McpServerConfigResult => ({ kind: \"invalid\", error });\n\tif (!SERVER_NAME.test(name)) return invalid(`invalid server name \"${name}\" (use letters, digits, \"_\" and \"-\")`);\n\tif (!isRecord(raw)) return invalid(`server \"${name}\" must be an object`);\n\tconst fieldsError = validateBaseFields(raw);\n\tif (fieldsError) return invalid(`server \"${name}\": ${fieldsError}`);\n\tconst { type, url, command } = raw;\n\tif (type === \"sse\") {\n\t\treturn invalid(`server \"${name}\": legacy SSE transport is not supported; use the streamable HTTP URL`);\n\t}\n\t// The casts below hold because the fields were checked one by one, which the types cannot express.\n\tif (typeof url === \"string\" && (type === undefined || type === \"http\" || type === \"streamable-http\")) {\n\t\tconst error = validateHttpFields(raw, url, options);\n\t\tif (error) return invalid(`server \"${name}\": ${error}`);\n\t\treturn { kind: \"http\", config: raw as McpHttpServerConfig & Record<string, unknown> };\n\t}\n\tif (typeof command === \"string\" && (type === undefined || type === \"stdio\")) {\n\t\tconst error = validateStdioFields(raw);\n\t\tif (error) return invalid(`server \"${name}\": ${error}`);\n\t\treturn { kind: \"stdio\", config: raw as McpStdioServerConfig & Record<string, unknown> };\n\t}\n\treturn invalid(`server \"${name}\" needs either \"command\" (stdio) or \"url\" (streamable HTTP)`);\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction validateBaseFields(entry: Record<string, unknown>): string | undefined {\n\tconst { enabled, timeout, description } = entry;\n\tif (enabled !== undefined && typeof enabled !== \"boolean\") return \"enabled must be a boolean\";\n\tif (description !== undefined && typeof description !== \"string\") return \"description must be a string\";\n\tif (timeout !== undefined && (typeof timeout !== \"number\" || !(timeout > 0))) {\n\t\treturn \"timeout must be a positive number of seconds\";\n\t}\n\treturn undefined;\n}\n\nfunction validateHttpFields(\n\tentry: Record<string, unknown>,\n\turl: string,\n\toptions: McpServerConfigOptions,\n): string | undefined {\n\tif (!URL.canParse(url) || !/^https?:$/.test(new URL(url).protocol)) return \"url must be an http or https URL\";\n\tif (entry.headers !== undefined && !isStringRecord(entry.headers)) return \"headers must map names to strings\";\n\treturn validateOAuth(entry.oauth, options);\n}\n\nfunction validateStdioFields(entry: Record<string, unknown>): string | undefined {\n\tconst { args, env, cwd } = entry;\n\tif (args !== undefined && !(Array.isArray(args) && args.every((arg) => typeof arg === \"string\"))) {\n\t\treturn \"args must be an array of strings\";\n\t}\n\tif (env !== undefined && !isStringRecord(env)) return \"env must map names to strings\";\n\tif (cwd !== undefined && typeof cwd !== \"string\") return \"cwd must be a string\";\n\treturn undefined;\n}\n\nfunction isStringRecord(value: unknown): value is Record<string, string> {\n\treturn isRecord(value) && Object.values(value).every((entry) => typeof entry === \"string\");\n}\n\nconst LOOPBACK_HOSTS = [\"localhost\", \"127.0.0.1\", \"[::1]\"];\n\nfunction validateOAuth(value: unknown, options: McpServerConfigOptions): string | undefined {\n\tif (value === undefined) return undefined;\n\tif (!isRecord(value)) return \"oauth must be an object\";\n\tif (value.clientId !== undefined && typeof value.clientId !== \"string\") return \"oauth.clientId must be a string\";\n\tif (value.clientSecret !== undefined && typeof value.clientSecret !== \"string\") {\n\t\treturn \"oauth.clientSecret must be a string\";\n\t}\n\tconst port = value.callbackPort;\n\tif (port !== undefined && (typeof port !== \"number\" || !Number.isInteger(port) || port < 1 || port > 65535)) {\n\t\treturn \"oauth.callbackPort must be a port number\";\n\t}\n\tif (value.callbackUrl !== undefined) {\n\t\tif (typeof value.callbackUrl !== \"string\" || !isLoopbackRedirectUri(value.callbackUrl)) {\n\t\t\treturn \"oauth.callbackUrl must be an http URI on localhost, 127.0.0.1, or [::1] without query or fragment\";\n\t\t}\n\t\tconst urlPort = new URL(value.callbackUrl).port;\n\t\tif (urlPort && port !== undefined && Number(urlPort) !== port) {\n\t\t\treturn \"oauth.callbackUrl and oauth.callbackPort name different ports\";\n\t\t}\n\t}\n\tif (value.scope !== undefined && typeof value.scope !== \"string\") return \"oauth.scope must be a string\";\n\tif (value.clientName !== undefined && (typeof value.clientName !== \"string\" || !value.clientName.trim())) {\n\t\treturn \"oauth.clientName must be a non-empty string\";\n\t}\n\tconst registration = value.clientRegistration;\n\tif (registration !== undefined && registration !== \"auto\" && registration !== \"dcr\") {\n\t\tif (registration !== \"cimd\") return 'oauth.clientRegistration must be \"auto\", \"dcr\", or \"cimd\"';\n\t\tif (value.clientId !== undefined || value.clientName !== undefined) {\n\t\t\treturn 'oauth.clientRegistration \"cimd\" cannot be combined with oauth.clientId or oauth.clientName';\n\t\t}\n\t\tif (!options.checkMetadataDocumentCallback) {\n\t\t\treturn 'oauth.clientRegistration \"cimd\" is not supported: there are no Client ID Metadata Documents';\n\t\t}\n\t\tconst callbackError = options.checkMetadataDocumentCallback(\n\t\t\ttypeof value.callbackUrl === \"string\" ? value.callbackUrl : undefined,\n\t\t);\n\t\tif (callbackError) return callbackError;\n\t}\n\tconst metadataUrl = value.authServerMetadataUrl;\n\tif (metadataUrl !== undefined) {\n\t\tconst url = typeof metadataUrl === \"string\" && URL.canParse(metadataUrl) ? new URL(metadataUrl) : undefined;\n\t\tif (!url || !(url.protocol === \"https:\" || (url.protocol === \"http:\" && LOOPBACK_HOSTS.includes(url.hostname)))) {\n\t\t\treturn \"oauth.authServerMetadataUrl must be an https URL, or http on localhost, 127.0.0.1, or [::1]\";\n\t\t}\n\t}\n\treturn undefined;\n}\n\n/** Whether a redirect URI can be served by a loopback callback server. */\nfunction isLoopbackRedirectUri(value: string): boolean {\n\tif (!URL.canParse(value)) return false;\n\tconst url = new URL(value);\n\treturn url.protocol === \"http:\" && LOOPBACK_HOSTS.includes(url.hostname) && url.search === \"\" && url.hash === \"\";\n}\n\n/**\n * Whether an entry overrides a server defined elsewhere instead of defining one: it has no `command`,\n * `url` or `type`. Applications let a less trusted file (a project's) override a few fields, such as\n * `enabled`, of a server a trusted file defines; which fields is theirs to decide.\n */\nexport function isMcpServerOverride(entry: Record<string, unknown>): boolean {\n\treturn entry.command === undefined && entry.url === undefined && entry.type === undefined;\n}\n"]}
@@ -0,0 +1,16 @@
1
+ export type { AuthProvider, McpFetch, UnauthorizedContext } from "./auth-provider.ts";
2
+ export { McpClient, type McpClientOptions, type McpRequestOptions } from "./client.ts";
3
+ export { isMcpServerOverride, type McpHttpServerConfig, type McpOAuthConfig, type McpServerConfig, type McpServerConfigOptions, type McpServerConfigResult, type McpStdioServerConfig, validateMcpServerConfig, } from "./config.ts";
4
+ export { normalizeMcpInputSchema } from "./input-schema.ts";
5
+ export { type McpModelContentOptions, toMcpModelContent } from "./model-content.ts";
6
+ export { createMcpToolName, mcpNamespace, mcpServerSummary } from "./naming.ts";
7
+ export { type LimitedMcpContent, limitMcpContent, MCP_OUTPUT_MAX_BYTES, type McpOutputSaver, } from "./output-limit.ts";
8
+ export { type AudioContent, type BlobResourceContents, type CallToolResult, type ContentAnnotations, type ContentBlock, type EmbeddedResourceContent, type ImageContent, type LlmContent, type ResourceLinkContent, type TextContent, type TextResourceContents, toLlmContent, } from "./protocol/content.ts";
9
+ export { isJsonRpcNotification, isJsonRpcRequest, isJsonRpcResponse, JSON_RPC_ERROR_CODES, type JsonRpcErrorObject, type JsonRpcErrorResponse, type JsonRpcId, type JsonRpcMessage, type JsonRpcNotification, type JsonRpcRequest, type JsonRpcResponse, type JsonRpcSuccessResponse, McpAbortError, McpConnectionClosedError, McpError, McpTimeoutError, parseJsonRpcMessage, } from "./protocol/jsonrpc.ts";
10
+ export { type CancelledNotification, type ClientCapabilities, type Implementation, type InitializeParams, type InitializeResult, LATEST_PROTOCOL_VERSION, type ListResourcesResult, type ListResourceTemplatesResult, type ListToolsResult, type ProgressNotification, type ReadResourceResult, type Resource, type ResourceTemplate, type Root, type ServerCapabilities, SUPPORTED_PROTOCOL_VERSIONS, type SupportedProtocolVersion, type Tool, type ToolAnnotations, type ToolExecution, } from "./protocol/types.ts";
11
+ export { createMcpResultSchema } from "./result-schema.ts";
12
+ export { type McpPipe, type McpPipeHandlers, PipeTransport, type PipeTransportOptions, } from "./transports/pipe.ts";
13
+ export { StdioTransport, type StdioTransportOptions } from "./transports/stdio.ts";
14
+ export { McpAuthRequiredError, McpHttpError, McpSessionExpiredError, StreamableHttpTransport, type StreamableHttpTransportOptions, } from "./transports/streamable-http.ts";
15
+ export type { McpTransport, McpTransportCloseListener, McpTransportErrorListener, McpTransportMessageListener, McpTransportSendOptions, } from "./transports/transport.ts";
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACtF,OAAO,EAAE,SAAS,EAAE,KAAK,gBAAgB,EAAE,KAAK,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACvF,OAAO,EACN,mBAAmB,EACnB,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,oBAAoB,EACzB,uBAAuB,GACvB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,KAAK,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACpF,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAChF,OAAO,EACN,KAAK,iBAAiB,EACtB,eAAe,EACf,oBAAoB,EACpB,KAAK,cAAc,GACnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,oBAAoB,EACzB,KAAK,cAAc,EACnB,KAAK,kBAAkB,EACvB,KAAK,YAAY,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AACzC,YAAY,EACX,YAAY,EACZ,yBAAyB,EACzB,yBAAyB,EACzB,2BAA2B,EAC3B,uBAAuB,GACvB,MAAM,2BAA2B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,14 @@
1
+ export { McpClient } from "./client.js";
2
+ export { isMcpServerOverride, validateMcpServerConfig, } from "./config.js";
3
+ export { normalizeMcpInputSchema } from "./input-schema.js";
4
+ export { toMcpModelContent } from "./model-content.js";
5
+ export { createMcpToolName, mcpNamespace, mcpServerSummary } from "./naming.js";
6
+ export { limitMcpContent, MCP_OUTPUT_MAX_BYTES, } from "./output-limit.js";
7
+ export { toLlmContent, } from "./protocol/content.js";
8
+ export { isJsonRpcNotification, isJsonRpcRequest, isJsonRpcResponse, JSON_RPC_ERROR_CODES, McpAbortError, McpConnectionClosedError, McpError, McpTimeoutError, parseJsonRpcMessage, } from "./protocol/jsonrpc.js";
9
+ export { LATEST_PROTOCOL_VERSION, SUPPORTED_PROTOCOL_VERSIONS, } from "./protocol/types.js";
10
+ export { createMcpResultSchema } from "./result-schema.js";
11
+ export { PipeTransport, } from "./transports/pipe.js";
12
+ export { StdioTransport } from "./transports/stdio.js";
13
+ export { McpAuthRequiredError, McpHttpError, McpSessionExpiredError, StreamableHttpTransport, } from "./transports/streamable-http.js";
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAiD,MAAM,aAAa,CAAC;AACvF,OAAO,EACN,mBAAmB,EAOnB,uBAAuB,GACvB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAA+B,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACpF,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAChF,OAAO,EAEN,eAAe,EACf,oBAAoB,GAEpB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAYN,YAAY,GACZ,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACN,qBAAqB,EACrB,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EASpB,aAAa,EACb,wBAAwB,EACxB,QAAQ,EACR,eAAe,EACf,mBAAmB,GACnB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAMN,uBAAuB,EAUvB,2BAA2B,GAK3B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAGN,aAAa,GAEb,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,cAAc,EAA8B,MAAM,uBAAuB,CAAC;AACnF,OAAO,EACN,oBAAoB,EACpB,YAAY,EACZ,sBAAsB,EACtB,uBAAuB,GAEvB,MAAM,iCAAiC,CAAC","sourcesContent":["export type { AuthProvider, McpFetch, UnauthorizedContext } from \"./auth-provider.ts\";\nexport { McpClient, type McpClientOptions, type McpRequestOptions } from \"./client.ts\";\nexport {\n\tisMcpServerOverride,\n\ttype McpHttpServerConfig,\n\ttype McpOAuthConfig,\n\ttype McpServerConfig,\n\ttype McpServerConfigOptions,\n\ttype McpServerConfigResult,\n\ttype McpStdioServerConfig,\n\tvalidateMcpServerConfig,\n} from \"./config.ts\";\nexport { normalizeMcpInputSchema } from \"./input-schema.ts\";\nexport { type McpModelContentOptions, toMcpModelContent } from \"./model-content.ts\";\nexport { createMcpToolName, mcpNamespace, mcpServerSummary } from \"./naming.ts\";\nexport {\n\ttype LimitedMcpContent,\n\tlimitMcpContent,\n\tMCP_OUTPUT_MAX_BYTES,\n\ttype McpOutputSaver,\n} from \"./output-limit.ts\";\nexport {\n\ttype AudioContent,\n\ttype BlobResourceContents,\n\ttype CallToolResult,\n\ttype ContentAnnotations,\n\ttype ContentBlock,\n\ttype EmbeddedResourceContent,\n\ttype ImageContent,\n\ttype LlmContent,\n\ttype ResourceLinkContent,\n\ttype TextContent,\n\ttype TextResourceContents,\n\ttoLlmContent,\n} from \"./protocol/content.ts\";\nexport {\n\tisJsonRpcNotification,\n\tisJsonRpcRequest,\n\tisJsonRpcResponse,\n\tJSON_RPC_ERROR_CODES,\n\ttype JsonRpcErrorObject,\n\ttype JsonRpcErrorResponse,\n\ttype JsonRpcId,\n\ttype JsonRpcMessage,\n\ttype JsonRpcNotification,\n\ttype JsonRpcRequest,\n\ttype JsonRpcResponse,\n\ttype JsonRpcSuccessResponse,\n\tMcpAbortError,\n\tMcpConnectionClosedError,\n\tMcpError,\n\tMcpTimeoutError,\n\tparseJsonRpcMessage,\n} from \"./protocol/jsonrpc.ts\";\nexport {\n\ttype CancelledNotification,\n\ttype ClientCapabilities,\n\ttype Implementation,\n\ttype InitializeParams,\n\ttype InitializeResult,\n\tLATEST_PROTOCOL_VERSION,\n\ttype ListResourcesResult,\n\ttype ListResourceTemplatesResult,\n\ttype ListToolsResult,\n\ttype ProgressNotification,\n\ttype ReadResourceResult,\n\ttype Resource,\n\ttype ResourceTemplate,\n\ttype Root,\n\ttype ServerCapabilities,\n\tSUPPORTED_PROTOCOL_VERSIONS,\n\ttype SupportedProtocolVersion,\n\ttype Tool,\n\ttype ToolAnnotations,\n\ttype ToolExecution,\n} from \"./protocol/types.ts\";\nexport { createMcpResultSchema } from \"./result-schema.ts\";\nexport {\n\ttype McpPipe,\n\ttype McpPipeHandlers,\n\tPipeTransport,\n\ttype PipeTransportOptions,\n} from \"./transports/pipe.ts\";\nexport { StdioTransport, type StdioTransportOptions } from \"./transports/stdio.ts\";\nexport {\n\tMcpAuthRequiredError,\n\tMcpHttpError,\n\tMcpSessionExpiredError,\n\tStreamableHttpTransport,\n\ttype StreamableHttpTransportOptions,\n} from \"./transports/streamable-http.ts\";\nexport type {\n\tMcpTransport,\n\tMcpTransportCloseListener,\n\tMcpTransportErrorListener,\n\tMcpTransportMessageListener,\n\tMcpTransportSendOptions,\n} from \"./transports/transport.ts\";\n"]}
@@ -0,0 +1,7 @@
1
+ /** An MCP tool's input schema in the shape LLM tool APIs accept. */
2
+ /**
3
+ * The tool's input schema as an object schema. Tool parameters must be objects, MCP servers may omit
4
+ * `type`, and some providers reject object schemas without `properties`. Other keywords are kept.
5
+ */
6
+ export declare function normalizeMcpInputSchema(schema: Record<string, unknown>): Record<string, unknown>;
7
+ //# sourceMappingURL=input-schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input-schema.d.ts","sourceRoot":"","sources":["../src/input-schema.ts"],"names":[],"mappings":"AAAA,oEAAoE;AAEpE;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAMhG"}
@@ -0,0 +1,13 @@
1
+ /** An MCP tool's input schema in the shape LLM tool APIs accept. */
2
+ /**
3
+ * The tool's input schema as an object schema. Tool parameters must be objects, MCP servers may omit
4
+ * `type`, and some providers reject object schemas without `properties`. Other keywords are kept.
5
+ */
6
+ export function normalizeMcpInputSchema(schema) {
7
+ return {
8
+ ...schema,
9
+ type: schema.type ?? "object",
10
+ ...(schema.properties === undefined ? { properties: {} } : {}),
11
+ };
12
+ }
13
+ //# sourceMappingURL=input-schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input-schema.js","sourceRoot":"","sources":["../src/input-schema.ts"],"names":[],"mappings":"AAAA,oEAAoE;AAEpE;;;GAGG;AACH,MAAM,UAAU,uBAAuB,CAAC,MAA+B;IACtE,OAAO;QACN,GAAG,MAAM;QACT,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,QAAQ;QAC7B,GAAG,CAAC,MAAM,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC9D,CAAC;AACH,CAAC","sourcesContent":["/** An MCP tool's input schema in the shape LLM tool APIs accept. */\n\n/**\n * The tool's input schema as an object schema. Tool parameters must be objects, MCP servers may omit\n * `type`, and some providers reject object schemas without `properties`. Other keywords are kept.\n */\nexport function normalizeMcpInputSchema(schema: Record<string, unknown>): Record<string, unknown> {\n\treturn {\n\t\t...schema,\n\t\ttype: schema.type ?? \"object\",\n\t\t...(schema.properties === undefined ? { properties: {} } : {}),\n\t};\n}\n"]}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The model-facing content of an MCP result as diffra's coding agent shows it: text and images, embedded text resources
3
+ * as text, binary resources saved where the model can read them, and resource links that name the tool reading them.
4
+ */
5
+ import type { McpOutputSaver } from "./output-limit.ts";
6
+ import { type CallToolResult, type LlmContent } from "./protocol/content.ts";
7
+ /** How {@link toMcpModelContent} handles what the model cannot read inline. */
8
+ export interface McpModelContentOptions {
9
+ /** Saves binary resources other than images where the model can read them. Without it they are left out. */
10
+ saveOutput?: McpOutputSaver;
11
+ /** The tool that reads the server's resources, which resource links then name. */
12
+ readResourceTool?: string;
13
+ }
14
+ /**
15
+ * Model-facing content of `server`'s result, before the output limit ({@link limitMcpContent}). A result without
16
+ * content blocks becomes its structured content as JSON, as in {@link toLlmContent}.
17
+ */
18
+ export declare function toMcpModelContent(server: string, result: Pick<CallToolResult, "content" | "structuredContent">, options?: McpModelContentOptions): Promise<LlmContent[]>;
19
+ //# sourceMappingURL=model-content.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-content.d.ts","sourceRoot":"","sources":["../src/model-content.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,KAAK,cAAc,EAAqB,KAAK,UAAU,EAAgB,MAAM,uBAAuB,CAAC;AAE9G,+EAA+E;AAC/E,MAAM,WAAW,sBAAsB;IACtC,4GAA4G;IAC5G,UAAU,CAAC,EAAE,cAAc,CAAC;IAC5B,kFAAkF;IAClF,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CACtC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,IAAI,CAAC,cAAc,EAAE,SAAS,GAAG,mBAAmB,CAAC,EAC7D,OAAO,GAAE,sBAA2B,GAClC,OAAO,CAAC,UAAU,EAAE,CAAC,CAGvB"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * The model-facing content of an MCP result as diffra's coding agent shows it: text and images, embedded text resources
3
+ * as text, binary resources saved where the model can read them, and resource links that name the tool reading them.
4
+ */
5
+ import { toLlmContent } from "./protocol/content.js";
6
+ /**
7
+ * Model-facing content of `server`'s result, before the output limit ({@link limitMcpContent}). A result without
8
+ * content blocks becomes its structured content as JSON, as in {@link toLlmContent}.
9
+ */
10
+ export async function toMcpModelContent(server, result, options = {}) {
11
+ if (result.content.length === 0)
12
+ return toLlmContent(result);
13
+ return (await Promise.all(result.content.map((block) => blockToContent(server, block, options)))).flat();
14
+ }
15
+ /** Model-facing content of one block of `server`'s result. */
16
+ async function blockToContent(server, block, options) {
17
+ if (block.type === "resource_link") {
18
+ const details = [block.mimeType, block.size === undefined ? undefined : formatSize(block.size)].filter(Boolean);
19
+ const read = options.readResourceTool ? `. Read it with ${options.readResourceTool} (server "${server}")` : "";
20
+ const description = block.description ? `: ${block.description}` : "";
21
+ return [
22
+ {
23
+ type: "text",
24
+ text: `[Resource ${block.uri} "${block.title ?? block.name}"${details.length > 0 ? ` (${details.join(", ")})` : ""}${description}${read}]`,
25
+ },
26
+ ];
27
+ }
28
+ if (block.type === "resource" && "blob" in block.resource && !block.resource.mimeType?.startsWith("image/")) {
29
+ const { uri, mimeType, blob } = block.resource;
30
+ const data = Buffer.from(blob, "base64");
31
+ if (isTextMimeType(mimeType))
32
+ return [{ type: "text", text: data.toString("utf8") }];
33
+ if (!options.saveOutput)
34
+ return toLlmContent({ content: [block] });
35
+ const kind = `${mimeType ?? "unknown type"}, ${formatSize(data.length)}`;
36
+ try {
37
+ const path = await options.saveOutput(data, extensionOf(uri));
38
+ return [{ type: "text", text: `[Binary resource ${uri} (${kind}) saved to ${path}]` }];
39
+ }
40
+ catch (error) {
41
+ const reason = error instanceof Error ? error.message : String(error);
42
+ return [{ type: "text", text: `[Binary resource ${uri} (${kind}) could not be saved: ${reason}]` }];
43
+ }
44
+ }
45
+ return toLlmContent({ content: [block] });
46
+ }
47
+ /** A byte count as diffra's coding agent shows sizes: `512B`, `2.0KB`, `1.5MB`. */
48
+ function formatSize(bytes) {
49
+ if (bytes < 1024)
50
+ return `${bytes}B`;
51
+ if (bytes < 1024 * 1024)
52
+ return `${(bytes / 1024).toFixed(1)}KB`;
53
+ return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
54
+ }
55
+ /** Blobs of these types are shown as text. */
56
+ function isTextMimeType(mimeType) {
57
+ if (!mimeType)
58
+ return false;
59
+ const type = mimeType.split(";", 1)[0].trim().toLowerCase();
60
+ return type.startsWith("text/") || type === "application/json" || type.endsWith("+json") || type.endsWith("+xml");
61
+ }
62
+ /** File extension for a saved binary resource: the one its URI ends in, else `.bin`. */
63
+ function extensionOf(uri) {
64
+ const path = URL.canParse(uri) ? new URL(uri).pathname : uri;
65
+ return /\.[A-Za-z0-9]{1,8}$/.exec(path)?.[0] ?? ".bin";
66
+ }
67
+ //# sourceMappingURL=model-content.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-content.js","sourceRoot":"","sources":["../src/model-content.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAA2D,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAU9G;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACtC,MAAc,EACd,MAA6D,EAC7D,OAAO,GAA2B,EAAE;IAEpC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,YAAY,CAAC,MAAM,CAAC,CAAC;IAC7D,OAAO,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,MAA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UAAU,CAAC,KAAa;IAChC,IAAI,KAAK,GAAG,IAAI;QAAE,OAAO,GAAG,KAAK,GAAG,CAAC;IACrC,IAAI,KAAK,GAAG,IAAI,GAAG,IAAI;QAAE,OAAO,GAAG,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;IACjE,OAAO,GAAG,CAAC,KAAK,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;AAClD,CAAC;AAED,8CAA8C;AAC9C,SAAS,cAAc,CAAC,QAA4B;IACnD,IAAI,CAAC,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5B,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAC5D,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,IAAI,KAAK,kBAAkB,IAAI,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AACnH,CAAC;AAED,wFAAwF;AACxF,SAAS,WAAW,CAAC,GAAW;IAC/B,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC;IAC7D,OAAO,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC;AACxD,CAAC","sourcesContent":["/**\n * The model-facing content of an MCP result as diffra's coding agent shows it: text and images, embedded text resources\n * as text, binary resources saved where the model can read them, and resource links that name the tool reading them.\n */\n\nimport type { McpOutputSaver } from \"./output-limit.ts\";\nimport { type CallToolResult, type ContentBlock, type LlmContent, toLlmContent } from \"./protocol/content.ts\";\n\n/** How {@link toMcpModelContent} handles what the model cannot read inline. */\nexport interface McpModelContentOptions {\n\t/** Saves binary resources other than images where the model can read them. Without it they are left out. */\n\tsaveOutput?: McpOutputSaver;\n\t/** The tool that reads the server's resources, which resource links then name. */\n\treadResourceTool?: string;\n}\n\n/**\n * Model-facing content of `server`'s result, before the output limit ({@link limitMcpContent}). A result without\n * content blocks becomes its structured content as JSON, as in {@link toLlmContent}.\n */\nexport async function toMcpModelContent(\n\tserver: string,\n\tresult: Pick<CallToolResult, \"content\" | \"structuredContent\">,\n\toptions: McpModelContentOptions = {},\n): Promise<LlmContent[]> {\n\tif (result.content.length === 0) return toLlmContent(result);\n\treturn (await Promise.all(result.content.map((block) => blockToContent(server, block, options)))).flat();\n}\n\n/** Model-facing content of one block of `server`'s result. */\nasync function blockToContent(\n\tserver: string,\n\tblock: ContentBlock,\n\toptions: McpModelContentOptions,\n): Promise<LlmContent[]> {\n\tif (block.type === \"resource_link\") {\n\t\tconst details = [block.mimeType, block.size === undefined ? undefined : formatSize(block.size)].filter(Boolean);\n\t\tconst read = options.readResourceTool ? `. Read it with ${options.readResourceTool} (server \"${server}\")` : \"\";\n\t\tconst description = block.description ? `: ${block.description}` : \"\";\n\t\treturn [\n\t\t\t{\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: `[Resource ${block.uri} \"${block.title ?? block.name}\"${details.length > 0 ? ` (${details.join(\", \")})` : \"\"}${description}${read}]`,\n\t\t\t},\n\t\t];\n\t}\n\tif (block.type === \"resource\" && \"blob\" in block.resource && !block.resource.mimeType?.startsWith(\"image/\")) {\n\t\tconst { uri, mimeType, blob } = block.resource;\n\t\tconst data = Buffer.from(blob, \"base64\");\n\t\tif (isTextMimeType(mimeType)) return [{ type: \"text\", text: data.toString(\"utf8\") }];\n\t\tif (!options.saveOutput) return toLlmContent({ content: [block] });\n\t\tconst kind = `${mimeType ?? \"unknown type\"}, ${formatSize(data.length)}`;\n\t\ttry {\n\t\t\tconst path = await options.saveOutput(data, extensionOf(uri));\n\t\t\treturn [{ type: \"text\", text: `[Binary resource ${uri} (${kind}) saved to ${path}]` }];\n\t\t} catch (error) {\n\t\t\tconst reason = error instanceof Error ? error.message : String(error);\n\t\t\treturn [{ type: \"text\", text: `[Binary resource ${uri} (${kind}) could not be saved: ${reason}]` }];\n\t\t}\n\t}\n\treturn toLlmContent({ content: [block] });\n}\n\n/** A byte count as diffra's coding agent shows sizes: `512B`, `2.0KB`, `1.5MB`. */\nfunction formatSize(bytes: number): string {\n\tif (bytes < 1024) return `${bytes}B`;\n\tif (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`;\n\treturn `${(bytes / (1024 * 1024)).toFixed(1)}MB`;\n}\n\n/** Blobs of these types are shown as text. */\nfunction isTextMimeType(mimeType: string | undefined): boolean {\n\tif (!mimeType) return false;\n\tconst type = mimeType.split(\";\", 1)[0].trim().toLowerCase();\n\treturn type.startsWith(\"text/\") || type === \"application/json\" || type.endsWith(\"+json\") || type.endsWith(\"+xml\");\n}\n\n/** File extension for a saved binary resource: the one its URI ends in, else `.bin`. */\nfunction extensionOf(uri: string): string {\n\tconst path = URL.canParse(uri) ? new URL(uri).pathname : uri;\n\treturn /\\.[A-Za-z0-9]{1,8}$/.exec(path)?.[0] ?? \".bin\";\n}\n"]}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Names of MCP tools and of their servers' namespaces, and the servers' one-line summaries, as models
3
+ * and code mode scripts see them. Applications that name tools the same way rank and group them the
4
+ * same way in tool search and code mode.
5
+ */
6
+ /**
7
+ * `mcp__<server>__<tool>`, sanitized and shortened with a hash suffix when too long. Like Codex,
8
+ * everything but `[A-Za-z0-9_]` becomes `_`, so the name is also the identifier code mode scripts
9
+ * call it by. `isTaken` reports names used by a different MCP tool: sanitizing can map two tools to
10
+ * one name (`a-b` and `a_b`), which then get the hash suffix.
11
+ */
12
+ export declare function createMcpToolName(server: string, tool: string, isTaken?: (name: string) => boolean): string;
13
+ /**
14
+ * Namespace of a server's tools: `mcp__<server>` with `-` replaced by `_`, like the tool names. Server
15
+ * names that differ only in `-` and `_` share a namespace, so a configuration must not have both.
16
+ */
17
+ export declare function mcpNamespace(server: string): string;
18
+ /**
19
+ * A server's one-line summary for the system prompt: the first line of its configured `description`,
20
+ * else of the instructions it sent in `initialize`; empty when it has neither.
21
+ */
22
+ export declare function mcpServerSummary(description: string | undefined, instructions: string | undefined): string;
23
+ //# sourceMappingURL=naming.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"naming.d.ts","sourceRoot":"","sources":["../src/naming.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAOH;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAqB,GAC9C,MAAM,CAKR;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,EAAE,YAAY,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAG1G"}
package/dist/naming.js ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Names of MCP tools and of their servers' namespaces, and the servers' one-line summaries, as models
3
+ * and code mode scripts see them. Applications that name tools the same way rank and group them the
4
+ * same way in tool search and code mode.
5
+ */
6
+ import { createHash } from "node:crypto";
7
+ /** Provider tool names are limited to 64 characters of `[A-Za-z0-9_-]`. */
8
+ const MAX_TOOL_NAME_LENGTH = 64;
9
+ /**
10
+ * `mcp__<server>__<tool>`, sanitized and shortened with a hash suffix when too long. Like Codex,
11
+ * everything but `[A-Za-z0-9_]` becomes `_`, so the name is also the identifier code mode scripts
12
+ * call it by. `isTaken` reports names used by a different MCP tool: sanitizing can map two tools to
13
+ * one name (`a-b` and `a_b`), which then get the hash suffix.
14
+ */
15
+ export function createMcpToolName(server, tool, isTaken = () => false) {
16
+ const name = `mcp__${server}__${tool}`.replace(/[^A-Za-z0-9_]/g, "_");
17
+ if (name.length <= MAX_TOOL_NAME_LENGTH && !isTaken(name))
18
+ return name;
19
+ const hash = createHash("sha256").update(`${server}\0${tool}`).digest("hex").slice(0, 8);
20
+ return `${name.slice(0, MAX_TOOL_NAME_LENGTH - hash.length - 1)}_${hash}`;
21
+ }
22
+ /**
23
+ * Namespace of a server's tools: `mcp__<server>` with `-` replaced by `_`, like the tool names. Server
24
+ * names that differ only in `-` and `_` share a namespace, so a configuration must not have both.
25
+ */
26
+ export function mcpNamespace(server) {
27
+ return `mcp__${server.replace(/-/g, "_")}`;
28
+ }
29
+ /**
30
+ * A server's one-line summary for the system prompt: the first line of its configured `description`,
31
+ * else of the instructions it sent in `initialize`; empty when it has neither.
32
+ */
33
+ export function mcpServerSummary(description, instructions) {
34
+ const text = description?.trim() || instructions?.trim() || "";
35
+ return (text.split("\n", 1)[0] ?? "").trim();
36
+ }
37
+ //# sourceMappingURL=naming.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"naming.js","sourceRoot":"","sources":["../src/naming.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,2EAA2E;AAC3E,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAEhC;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAChC,MAAc,EACd,IAAY,EACZ,OAAO,GAA8B,GAAG,EAAE,CAAC,KAAK;IAEhD,MAAM,IAAI,GAAG,QAAQ,MAAM,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC;IACtE,IAAI,IAAI,CAAC,MAAM,IAAI,oBAAoB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IACvE,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,GAAG,MAAM,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACzF,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,oBAAoB,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC;AAC3E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,MAAc;IAC1C,OAAO,QAAQ,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC;AAC5C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,WAA+B,EAAE,YAAgC;IACjG,MAAM,IAAI,GAAG,WAAW,EAAE,IAAI,EAAE,IAAI,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IAC/D,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;AAC9C,CAAC","sourcesContent":["/**\n * Names of MCP tools and of their servers' namespaces, and the servers' one-line summaries, as models\n * and code mode scripts see them. Applications that name tools the same way rank and group them the\n * same way in tool search and code mode.\n */\n\nimport { createHash } from \"node:crypto\";\n\n/** Provider tool names are limited to 64 characters of `[A-Za-z0-9_-]`. */\nconst MAX_TOOL_NAME_LENGTH = 64;\n\n/**\n * `mcp__<server>__<tool>`, sanitized and shortened with a hash suffix when too long. Like Codex,\n * everything but `[A-Za-z0-9_]` becomes `_`, so the name is also the identifier code mode scripts\n * call it by. `isTaken` reports names used by a different MCP tool: sanitizing can map two tools to\n * one name (`a-b` and `a_b`), which then get the hash suffix.\n */\nexport function createMcpToolName(\n\tserver: string,\n\ttool: string,\n\tisTaken: (name: string) => boolean = () => false,\n): string {\n\tconst name = `mcp__${server}__${tool}`.replace(/[^A-Za-z0-9_]/g, \"_\");\n\tif (name.length <= MAX_TOOL_NAME_LENGTH && !isTaken(name)) return name;\n\tconst hash = createHash(\"sha256\").update(`${server}\\0${tool}`).digest(\"hex\").slice(0, 8);\n\treturn `${name.slice(0, MAX_TOOL_NAME_LENGTH - hash.length - 1)}_${hash}`;\n}\n\n/**\n * Namespace of a server's tools: `mcp__<server>` with `-` replaced by `_`, like the tool names. Server\n * names that differ only in `-` and `_` share a namespace, so a configuration must not have both.\n */\nexport function mcpNamespace(server: string): string {\n\treturn `mcp__${server.replace(/-/g, \"_\")}`;\n}\n\n/**\n * A server's one-line summary for the system prompt: the first line of its configured `description`,\n * else of the instructions it sent in `initialize`; empty when it has neither.\n */\nexport function mcpServerSummary(description: string | undefined, instructions: string | undefined): string {\n\tconst text = description?.trim() || instructions?.trim() || \"\";\n\treturn (text.split(\"\\n\", 1)[0] ?? \"\").trim();\n}\n"]}