@dev-crowagent/pigeon-mcp 0.0.0-stage → 1.7.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 (136) hide show
  1. package/CHANGELOG.md +143 -0
  2. package/README.md +256 -2
  3. package/THIRD_PARTY_NOTICES/modelcontextprotocol-typescript-sdk.txt +21 -0
  4. package/dist/auth-provider.d.ts +19 -0
  5. package/dist/auth-provider.d.ts.map +1 -0
  6. package/dist/auth-provider.js +2 -0
  7. package/dist/auth-provider.js.map +1 -0
  8. package/dist/client.d.ts +193 -0
  9. package/dist/client.d.ts.map +1 -0
  10. package/dist/client.js +747 -0
  11. package/dist/client.js.map +1 -0
  12. package/dist/config-schema.d.ts +105 -0
  13. package/dist/config-schema.d.ts.map +1 -0
  14. package/dist/config-schema.js +101 -0
  15. package/dist/config-schema.js.map +1 -0
  16. package/dist/config.d.ts +57 -0
  17. package/dist/config.d.ts.map +1 -0
  18. package/dist/config.js +160 -0
  19. package/dist/config.js.map +1 -0
  20. package/dist/index.d.ts +16 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +14 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/input-schema.d.ts +7 -0
  25. package/dist/input-schema.d.ts.map +1 -0
  26. package/dist/input-schema.js +13 -0
  27. package/dist/input-schema.js.map +1 -0
  28. package/dist/model-content.d.ts +19 -0
  29. package/dist/model-content.d.ts.map +1 -0
  30. package/dist/model-content.js +67 -0
  31. package/dist/model-content.js.map +1 -0
  32. package/dist/naming.d.ts +23 -0
  33. package/dist/naming.d.ts.map +1 -0
  34. package/dist/naming.js +37 -0
  35. package/dist/naming.js.map +1 -0
  36. package/dist/oauth/auth-provider.d.ts +52 -0
  37. package/dist/oauth/auth-provider.d.ts.map +1 -0
  38. package/dist/oauth/auth-provider.js +97 -0
  39. package/dist/oauth/auth-provider.js.map +1 -0
  40. package/dist/oauth/callback.d.ts +48 -0
  41. package/dist/oauth/callback.d.ts.map +1 -0
  42. package/dist/oauth/callback.js +121 -0
  43. package/dist/oauth/callback.js.map +1 -0
  44. package/dist/oauth/client-settings.d.ts +107 -0
  45. package/dist/oauth/client-settings.d.ts.map +1 -0
  46. package/dist/oauth/client-settings.js +114 -0
  47. package/dist/oauth/client-settings.js.map +1 -0
  48. package/dist/oauth/discovery.d.ts +30 -0
  49. package/dist/oauth/discovery.d.ts.map +1 -0
  50. package/dist/oauth/discovery.js +156 -0
  51. package/dist/oauth/discovery.js.map +1 -0
  52. package/dist/oauth/errors.d.ts +32 -0
  53. package/dist/oauth/errors.d.ts.map +1 -0
  54. package/dist/oauth/errors.js +58 -0
  55. package/dist/oauth/errors.js.map +1 -0
  56. package/dist/oauth/flow.d.ts +112 -0
  57. package/dist/oauth/flow.d.ts.map +1 -0
  58. package/dist/oauth/flow.js +360 -0
  59. package/dist/oauth/flow.js.map +1 -0
  60. package/dist/oauth/index.d.ts +11 -0
  61. package/dist/oauth/index.d.ts.map +1 -0
  62. package/dist/oauth/index.js +9 -0
  63. package/dist/oauth/index.js.map +1 -0
  64. package/dist/oauth/loopback.d.ts +51 -0
  65. package/dist/oauth/loopback.d.ts.map +1 -0
  66. package/dist/oauth/loopback.js +98 -0
  67. package/dist/oauth/loopback.js.map +1 -0
  68. package/dist/oauth/provider.d.ts +62 -0
  69. package/dist/oauth/provider.d.ts.map +1 -0
  70. package/dist/oauth/provider.js +118 -0
  71. package/dist/oauth/provider.js.map +1 -0
  72. package/dist/oauth/sign-in.d.ts +73 -0
  73. package/dist/oauth/sign-in.d.ts.map +1 -0
  74. package/dist/oauth/sign-in.js +182 -0
  75. package/dist/oauth/sign-in.js.map +1 -0
  76. package/dist/oauth/types.d.ts +83 -0
  77. package/dist/oauth/types.d.ts.map +1 -0
  78. package/dist/oauth/types.js +113 -0
  79. package/dist/oauth/types.js.map +1 -0
  80. package/dist/output-limit.d.ts +24 -0
  81. package/dist/output-limit.d.ts.map +1 -0
  82. package/dist/output-limit.js +58 -0
  83. package/dist/output-limit.js.map +1 -0
  84. package/dist/protocol/content.d.ts +83 -0
  85. package/dist/protocol/content.d.ts.map +1 -0
  86. package/dist/protocol/content.js +41 -0
  87. package/dist/protocol/content.js.map +1 -0
  88. package/dist/protocol/http-headers.d.ts +31 -0
  89. package/dist/protocol/http-headers.d.ts.map +1 -0
  90. package/dist/protocol/http-headers.js +129 -0
  91. package/dist/protocol/http-headers.js.map +1 -0
  92. package/dist/protocol/jsonrpc.d.ts +64 -0
  93. package/dist/protocol/jsonrpc.d.ts.map +1 -0
  94. package/dist/protocol/jsonrpc.js +74 -0
  95. package/dist/protocol/jsonrpc.js.map +1 -0
  96. package/dist/protocol/stateless.d.ts +47 -0
  97. package/dist/protocol/stateless.d.ts.map +1 -0
  98. package/dist/protocol/stateless.js +102 -0
  99. package/dist/protocol/stateless.js.map +1 -0
  100. package/dist/protocol/types.d.ts +156 -0
  101. package/dist/protocol/types.d.ts.map +1 -0
  102. package/dist/protocol/types.js +12 -0
  103. package/dist/protocol/types.js.map +1 -0
  104. package/dist/result-schema.d.ts +10 -0
  105. package/dist/result-schema.d.ts.map +1 -0
  106. package/dist/result-schema.js +78 -0
  107. package/dist/result-schema.js.map +1 -0
  108. package/dist/testing/index.d.ts +2 -0
  109. package/dist/testing/index.d.ts.map +1 -0
  110. package/dist/testing/index.js +2 -0
  111. package/dist/testing/index.js.map +1 -0
  112. package/dist/transports/in-memory.d.ts +19 -0
  113. package/dist/transports/in-memory.d.ts.map +1 -0
  114. package/dist/transports/in-memory.js +50 -0
  115. package/dist/transports/in-memory.js.map +1 -0
  116. package/dist/transports/lines.d.ts +34 -0
  117. package/dist/transports/lines.d.ts.map +1 -0
  118. package/dist/transports/lines.js +85 -0
  119. package/dist/transports/lines.js.map +1 -0
  120. package/dist/transports/pipe.d.ts +49 -0
  121. package/dist/transports/pipe.d.ts.map +1 -0
  122. package/dist/transports/pipe.js +96 -0
  123. package/dist/transports/pipe.js.map +1 -0
  124. package/dist/transports/stdio.d.ts +29 -0
  125. package/dist/transports/stdio.d.ts.map +1 -0
  126. package/dist/transports/stdio.js +170 -0
  127. package/dist/transports/stdio.js.map +1 -0
  128. package/dist/transports/streamable-http.d.ts +113 -0
  129. package/dist/transports/streamable-http.d.ts.map +1 -0
  130. package/dist/transports/streamable-http.js +586 -0
  131. package/dist/transports/streamable-http.js.map +1 -0
  132. package/dist/transports/transport.d.ts +48 -0
  133. package/dist/transports/transport.d.ts.map +1 -0
  134. package/dist/transports/transport.js +38 -0
  135. package/dist/transports/transport.js.map +1 -0
  136. package/package.json +65 -5
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AA4BjE,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;QAAE,OAAO,OAAO,CAAC,WAAW,IAAI,MAAM,WAAW,EAAE,CAAC,CAAC;IACpE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,GAAG,CAAC;IACnC,IAAI,IAAI,KAAK,KAAK,EAAE,CAAC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copied between\n * them: stdio servers (`command`, `args`, `env`, `cwd`), Streamable HTTP servers (`url`, `headers`,\n * `oauth`), and `enabled`, `description`, `timeout` and `protocol` 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 * The shape itself lives in `config-schema.ts`: a JSON Schema the configuration types derive from,\n * whose parts applications compose into published schemas for their config files.\n */\n\nimport type { McpHttpServerConfig, McpStdioServerConfig } from \"./config-schema.ts\";\nimport { STATELESS_PROTOCOL_VERSION } from \"./protocol/types.ts\";\n\nexport type {\n\tMcpHttpServerConfig,\n\tMcpOAuthConfig,\n\tMcpServerConfig,\n\tMcpStdioServerConfig,\n} from \"./config-schema.ts\";\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, protocol } = entry;\n\tif (enabled !== undefined && typeof enabled !== \"boolean\") return \"enabled must be a boolean\";\n\tif (protocol !== undefined && protocol !== \"auto\" && protocol !== STATELESS_PROTOCOL_VERSION) {\n\t\treturn `protocol must be \"auto\" or \"${STATELESS_PROTOCOL_VERSION}\"`;\n\t}\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 McpExtensionResultHandler, McpInitializeRejectedError, type McpRequestOptions, type McpSubscription, } 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 LoggingLevel, type McpProtocolMode, type ProgressNotification, type ReadResourceResult, type Resource, type ResourceTemplate, type Root, type ServerCapabilities, STATELESS_PROTOCOL_VERSION, SUPPORTED_PROTOCOL_VERSIONS, type SubscriptionFilter, 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,EACN,SAAS,EACT,KAAK,gBAAgB,EACrB,KAAK,yBAAyB,EAC9B,0BAA0B,EAC1B,KAAK,iBAAiB,EACtB,KAAK,eAAe,GACpB,MAAM,aAAa,CAAC;AACrB,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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B,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EACN,KAAK,OAAO,EACZ,KAAK,eAAe,EACpB,aAAa,EACb,KAAK,oBAAoB,GACzB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,cAAc,EAAE,KAAK,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AACnF,OAAO,EACN,oBAAoB,EACpB,YAAY,EACZ,sBAAsB,EACtB,uBAAuB,EACvB,KAAK,8BAA8B,GACnC,MAAM,iCAAiC,CAAC;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, McpInitializeRejectedError, } 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, STATELESS_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,EACN,SAAS,EAGT,0BAA0B,GAG1B,MAAM,aAAa,CAAC;AACrB,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,EAYvB,0BAA0B,EAC1B,2BAA2B,GAM3B,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 {\n\tMcpClient,\n\ttype McpClientOptions,\n\ttype McpExtensionResultHandler,\n\tMcpInitializeRejectedError,\n\ttype McpRequestOptions,\n\ttype McpSubscription,\n} 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 LoggingLevel,\n\ttype McpProtocolMode,\n\ttype ProgressNotification,\n\ttype ReadResourceResult,\n\ttype Resource,\n\ttype ResourceTemplate,\n\ttype Root,\n\ttype ServerCapabilities,\n\tSTATELESS_PROTOCOL_VERSION,\n\tSUPPORTED_PROTOCOL_VERSIONS,\n\ttype SubscriptionFilter,\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 pigeon'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 pigeon'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 pigeon'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,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,cAAc,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AAC1G,CAAC;AAED,8DAA8D;AAC9D,KAAK,UAAU,cAAc,CAC5B,MAAc,EACd,KAAmB,EACnB,OAA+B;IAE/B,IAAI,KAAK,CAAC,IAAI,KAAK,eAAe,EAAE,CAAC;QACpC,MAAM,OAAO,GAAG,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAChH,MAAM,IAAI,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,kBAAkB,OAAO,CAAC,gBAAgB,aAAa,MAAM,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/G,MAAM,WAAW,GAAG,KAAK,CAA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,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 pigeon'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 pigeon'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 +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E,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"]}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The auth provider for connections to an OAuth-protected MCP server: it sends the stored access token
3
+ * and refreshes it, but never starts a browser flow. When the user has to sign in, it fails with
4
+ * `McpOAuthAuthorizationRequiredError` and the application runs `startMcpSignIn`.
5
+ */
6
+ import type { AuthProvider, McpFetch } from "../auth-provider.ts";
7
+ import { type McpOAuthClientOptions, type McpOAuthSettings } from "./client-settings.ts";
8
+ import { type OAuthFlowResult } from "./flow.ts";
9
+ import type { McpOAuthState } from "./provider.ts";
10
+ import type { OAuthChallenge } from "./types.ts";
11
+ /** An `AuthProvider` that can say when its refreshes have finished. */
12
+ export interface McpAuthProvider extends AuthProvider {
13
+ /** Resolves when no refresh is running, so shutdown does not drop rotated tokens before they are saved. */
14
+ settled(): Promise<void>;
15
+ }
16
+ /**
17
+ * Auth provider for MCP connections: sends the stored access token and refreshes it when it is about
18
+ * to expire or after a 401. Throws `McpOAuthAuthorizationRequiredError` when the user has to sign in,
19
+ * including when the server asks for more scope (`insufficient_scope`). `onChallenge` receives the
20
+ * server's `WWW-Authenticate` challenge so sign-in can use its resource metadata URL and scope.
21
+ * `settings` is only called when a refresh is needed, so a secret that fails to resolve fails the
22
+ * refresh instead of the whole connection setup.
23
+ *
24
+ * Many servers rotate refresh tokens, so two refreshes with the same refresh token lose the grant.
25
+ * Requests in this process share one refresh, and other processes are kept out by the store's
26
+ * refresh lock, held from reading the tokens to saving new ones. Tokens that changed meanwhile
27
+ * (another process refreshed them, or the user signed in) are used without refreshing.
28
+ */
29
+ export declare function createMcpAuthProvider(options: Omit<McpOAuthClientOptions, "settings"> & {
30
+ settings: () => McpOAuthSettings;
31
+ onChallenge: (challenge: OAuthChallenge) => void;
32
+ /**
33
+ * A redirect URI the application signs in with, such as its https callback route. Refreshing tokens
34
+ * from a sign-in with a Client ID Metadata Document needs a redirect URI the documents list; a
35
+ * refresh never redirects, so any listed one works. Default: the loopback redirect URI of `settings`.
36
+ */
37
+ redirectUrl?: string;
38
+ }): McpAuthProvider;
39
+ /**
40
+ * Refresh the tokens of `state` with the stored client (or the document it signed in with), or report
41
+ * `REDIRECT` when that is not possible. `options.redirectUrl` is a redirect URI the application signs in
42
+ * with, tried before the loopback ones of the settings and the stored client. Never registers a client or
43
+ * prepares an authorization request, so a pending sign-in stays usable. Call while holding the store's
44
+ * refresh lock. Not cancellable: a refresh the server answered may have rotated the refresh token, so its
45
+ * answer must be saved.
46
+ */
47
+ export declare function refreshStoredTokens(client: McpOAuthClientOptions, state: McpOAuthState, options?: {
48
+ fetch?: McpFetch;
49
+ challenge?: OAuthChallenge;
50
+ redirectUrl?: string;
51
+ }): Promise<OAuthFlowResult>;
52
+ //# sourceMappingURL=auth-provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-provider.d.ts","sourceRoot":"","sources":["../../src/oauth/auth-provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAIN,KAAK,qBAAqB,EAC1B,KAAK,gBAAgB,EAGrB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAgB,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAC/D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,uEAAuE;AACvE,MAAM,WAAW,eAAgB,SAAQ,YAAY;IACpD,2GAA2G;IAC3G,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB;AAKD;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,IAAI,CAAC,qBAAqB,EAAE,UAAU,CAAC,GAAG;IAClD,QAAQ,EAAE,MAAM,gBAAgB,CAAC;IACjC,WAAW,EAAE,CAAC,SAAS,EAAE,cAAc,KAAK,IAAI,CAAC;IACjD;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,GACC,eAAe,CA2CjB;AAED;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CACxC,MAAM,EAAE,qBAAqB,EAC7B,KAAK,EAAE,aAAa,EACpB,OAAO,GAAE;IAAE,KAAK,CAAC,EAAE,QAAQ,CAAC;IAAC,SAAS,CAAC,EAAE,cAAc,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAO,GAClF,OAAO,CAAC,eAAe,CAAC,CAiB1B"}
@@ -0,0 +1,97 @@
1
+ /**
2
+ * The auth provider for connections to an OAuth-protected MCP server: it sends the stored access token
3
+ * and refreshes it, but never starts a browser flow. When the user has to sign in, it fails with
4
+ * `McpOAuthAuthorizationRequiredError` and the application runs `startMcpSignIn`.
5
+ */
6
+ import { createOAuthClientProvider, DEFAULT_REDIRECT_URL, loopbackSettings, registeredRedirectUrls, timedFetch, } from "./client-settings.js";
7
+ import { parseWwwAuthenticate } from "./discovery.js";
8
+ import { McpOAuthAuthorizationRequiredError } from "./errors.js";
9
+ import { authorizeMcp } from "./flow.js";
10
+ /** Access tokens this close to expiry are refreshed before they are sent. */
11
+ const REFRESH_SKEW_MS = 30_000;
12
+ /**
13
+ * Auth provider for MCP connections: sends the stored access token and refreshes it when it is about
14
+ * to expire or after a 401. Throws `McpOAuthAuthorizationRequiredError` when the user has to sign in,
15
+ * including when the server asks for more scope (`insufficient_scope`). `onChallenge` receives the
16
+ * server's `WWW-Authenticate` challenge so sign-in can use its resource metadata URL and scope.
17
+ * `settings` is only called when a refresh is needed, so a secret that fails to resolve fails the
18
+ * refresh instead of the whole connection setup.
19
+ *
20
+ * Many servers rotate refresh tokens, so two refreshes with the same refresh token lose the grant.
21
+ * Requests in this process share one refresh, and other processes are kept out by the store's
22
+ * refresh lock, held from reading the tokens to saving new ones. Tokens that changed meanwhile
23
+ * (another process refreshed them, or the user signed in) are used without refreshing.
24
+ */
25
+ export function createMcpAuthProvider(options) {
26
+ const { store, redirectUrl } = options;
27
+ let refreshing;
28
+ /** Replace `staleToken`, the access token that expired or was rejected. */
29
+ const refresh = (staleToken, fetch = globalThis.fetch, challenge) => {
30
+ refreshing ??= store
31
+ .withRefreshLock(async () => {
32
+ const state = await store.load();
33
+ if (state?.tokens?.access_token !== staleToken)
34
+ return;
35
+ if (!state?.tokens?.refresh_token)
36
+ throw new McpOAuthAuthorizationRequiredError();
37
+ const client = { ...options, settings: options.settings() };
38
+ const result = await refreshStoredTokens(client, state, { fetch, challenge, redirectUrl });
39
+ if (result === "REDIRECT")
40
+ throw new McpOAuthAuthorizationRequiredError();
41
+ })
42
+ .finally(() => {
43
+ refreshing = undefined;
44
+ });
45
+ return refreshing;
46
+ };
47
+ return {
48
+ token: async () => {
49
+ await refreshing?.catch(() => undefined);
50
+ const state = await store.load();
51
+ const token = state?.tokens?.access_token;
52
+ const expired = state?.tokensExpireAt !== undefined && state.tokensExpireAt - REFRESH_SKEW_MS <= Date.now();
53
+ if (!expired || !state?.tokens?.refresh_token)
54
+ return token;
55
+ // Failures fall through: the request goes out with the old token and a 401 decides what happens.
56
+ await refresh(token).catch(() => undefined);
57
+ return (await store.load())?.tokens?.access_token;
58
+ },
59
+ onUnauthorized: async (context) => {
60
+ const challenge = parseWwwAuthenticate(context.response.headers.get("www-authenticate"));
61
+ options.onChallenge(challenge);
62
+ // A refresh keeps the granted scope, so more scope needs a new sign-in.
63
+ if (challenge.error === "insufficient_scope")
64
+ throw new McpOAuthAuthorizationRequiredError();
65
+ await refresh(context.token, context.fetch, challenge);
66
+ },
67
+ settled: async () => {
68
+ await refreshing?.catch(() => undefined);
69
+ },
70
+ };
71
+ }
72
+ /**
73
+ * Refresh the tokens of `state` with the stored client (or the document it signed in with), or report
74
+ * `REDIRECT` when that is not possible. `options.redirectUrl` is a redirect URI the application signs in
75
+ * with, tried before the loopback ones of the settings and the stored client. Never registers a client or
76
+ * prepares an authorization request, so a pending sign-in stays usable. Call while holding the store's
77
+ * refresh lock. Not cancellable: a refresh the server answered may have rotated the refresh token, so its
78
+ * answer must be saved.
79
+ */
80
+ export async function refreshStoredTokens(client, state, options = {}) {
81
+ // Refreshing never redirects the user; the redirect URI only decides whether tokens without a stored
82
+ // client can be refreshed with a Client ID Metadata Document, so any URI the documents list works.
83
+ const candidates = [
84
+ options.redirectUrl,
85
+ loopbackSettings(client.settings).fixedRedirectUrl,
86
+ registeredRedirectUrls(state.clientInformation)[0],
87
+ ].filter((url) => url !== undefined);
88
+ const redirectUrl = candidates.find((url) => client.clientMetadataDocuments?.listsRedirectUrl(url)) ?? DEFAULT_REDIRECT_URL;
89
+ return authorizeMcp(createOAuthClientProvider(client, redirectUrl), {
90
+ serverUrl: client.serverUrl,
91
+ resourceMetadataUrl: options.challenge?.resourceMetadataUrl,
92
+ authorizationServerMetadataUrl: client.settings.authServerMetadataUrl,
93
+ fetch: timedFetch(options.fetch),
94
+ refreshOnly: true,
95
+ });
96
+ }
97
+ //# sourceMappingURL=auth-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-provider.js","sourceRoot":"","sources":["../../src/oauth/auth-provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EACN,yBAAyB,EACzB,oBAAoB,EACpB,gBAAgB,EAGhB,sBAAsB,EACtB,UAAU,GACV,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,kCAAkC,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,YAAY,EAAwB,MAAM,WAAW,CAAC;AAU/D,6EAA6E;AAC7E,MAAM,eAAe,GAAG,MAAM,CAAC;AAE/B;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,qBAAqB,CACpC,OASC;IAED,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;IACvC,IAAI,UAAqC,CAAC;IAE1C,2EAA2E;IAC3E,MAAM,OAAO,GAAG,CAAC,UAA8B,EAAE,KAAK,GAAa,UAAU,CAAC,KAAK,EAAE,SAA0B,EAAE,EAAE;QAClH,UAAU,KAAK,KAAK;aAClB,eAAe,CAAC,KAAK,IA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B,CAAC,MAAM,EAAE,WAAW,CAAC,EAAE;QACnE,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,mBAAmB,EAAE,OAAO,CAAC,SAAS,EAAE,mBAAmB;QAC3D,8BAA8B,EAAE,MAAM,CAAC,QAAQ,CAAC,qBAAqB;QACrE,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC;QAChC,WAAW,EAAE,IAAI;KACjB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * The auth provider for connections to an OAuth-protected MCP server: it sends the stored access token\n * and refreshes it, but never starts a browser flow. When the user has to sign in, it fails with\n * `McpOAuthAuthorizationRequiredError` and the application runs `startMcpSignIn`.\n */\n\nimport type { AuthProvider, McpFetch } from \"../auth-provider.ts\";\nimport {\n\tcreateOAuthClientProvider,\n\tDEFAULT_REDIRECT_URL,\n\tloopbackSettings,\n\ttype McpOAuthClientOptions,\n\ttype McpOAuthSettings,\n\tregisteredRedirectUrls,\n\ttimedFetch,\n} from \"./client-settings.ts\";\nimport { parseWwwAuthenticate } from \"./discovery.ts\";\nimport { McpOAuthAuthorizationRequiredError } from \"./errors.ts\";\nimport { authorizeMcp, type OAuthFlowResult } from \"./flow.ts\";\nimport type { McpOAuthState } from \"./provider.ts\";\nimport type { OAuthChallenge } from \"./types.ts\";\n\n/** An `AuthProvider` that can say when its refreshes have finished. */\nexport interface McpAuthProvider extends AuthProvider {\n\t/** Resolves when no refresh is running, so shutdown does not drop rotated tokens before they are saved. */\n\tsettled(): Promise<void>;\n}\n\n/** Access tokens this close to expiry are refreshed before they are sent. */\nconst REFRESH_SKEW_MS = 30_000;\n\n/**\n * Auth provider for MCP connections: sends the stored access token and refreshes it when it is about\n * to expire or after a 401. Throws `McpOAuthAuthorizationRequiredError` when the user has to sign in,\n * including when the server asks for more scope (`insufficient_scope`). `onChallenge` receives the\n * server's `WWW-Authenticate` challenge so sign-in can use its resource metadata URL and scope.\n * `settings` is only called when a refresh is needed, so a secret that fails to resolve fails the\n * refresh instead of the whole connection setup.\n *\n * Many servers rotate refresh tokens, so two refreshes with the same refresh token lose the grant.\n * Requests in this process share one refresh, and other processes are kept out by the store's\n * refresh lock, held from reading the tokens to saving new ones. Tokens that changed meanwhile\n * (another process refreshed them, or the user signed in) are used without refreshing.\n */\nexport function createMcpAuthProvider(\n\toptions: Omit<McpOAuthClientOptions, \"settings\"> & {\n\t\tsettings: () => McpOAuthSettings;\n\t\tonChallenge: (challenge: OAuthChallenge) => void;\n\t\t/**\n\t\t * A redirect URI the application signs in with, such as its https callback route. Refreshing tokens\n\t\t * from a sign-in with a Client ID Metadata Document needs a redirect URI the documents list; a\n\t\t * refresh never redirects, so any listed one works. Default: the loopback redirect URI of `settings`.\n\t\t */\n\t\tredirectUrl?: string;\n\t},\n): McpAuthProvider {\n\tconst { store, redirectUrl } = options;\n\tlet refreshing: Promise<void> | undefined;\n\n\t/** Replace `staleToken`, the access token that expired or was rejected. */\n\tconst refresh = (staleToken: string | undefined, fetch: McpFetch = globalThis.fetch, challenge?: OAuthChallenge) => {\n\t\trefreshing ??= store\n\t\t\t.withRefreshLock(async () => {\n\t\t\t\tconst state = await store.load();\n\t\t\t\tif (state?.tokens?.access_token !== staleToken) return;\n\t\t\t\tif (!state?.tokens?.refresh_token) throw new McpOAuthAuthorizationRequiredError();\n\t\t\t\tconst client = { ...options, settings: options.settings() };\n\t\t\t\tconst result = await refreshStoredTokens(client, state, { fetch, challenge, redirectUrl });\n\t\t\t\tif (result === \"REDIRECT\") throw new McpOAuthAuthorizationRequiredError();\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\trefreshing = undefined;\n\t\t\t});\n\t\treturn refreshing;\n\t};\n\n\treturn {\n\t\ttoken: async () => {\n\t\t\tawait refreshing?.catch(() => undefined);\n\t\t\tconst state = await store.load();\n\t\t\tconst token = state?.tokens?.access_token;\n\t\t\tconst expired = state?.tokensExpireAt !== undefined && state.tokensExpireAt - REFRESH_SKEW_MS <= Date.now();\n\t\t\tif (!expired || !state?.tokens?.refresh_token) return token;\n\t\t\t// Failures fall through: the request goes out with the old token and a 401 decides what happens.\n\t\t\tawait refresh(token).catch(() => undefined);\n\t\t\treturn (await store.load())?.tokens?.access_token;\n\t\t},\n\t\tonUnauthorized: async (context) => {\n\t\t\tconst challenge = parseWwwAuthenticate(context.response.headers.get(\"www-authenticate\"));\n\t\t\toptions.onChallenge(challenge);\n\t\t\t// A refresh keeps the granted scope, so more scope needs a new sign-in.\n\t\t\tif (challenge.error === \"insufficient_scope\") throw new McpOAuthAuthorizationRequiredError();\n\t\t\tawait refresh(context.token, context.fetch, challenge);\n\t\t},\n\t\tsettled: async () => {\n\t\t\tawait refreshing?.catch(() => undefined);\n\t\t},\n\t};\n}\n\n/**\n * Refresh the tokens of `state` with the stored client (or the document it signed in with), or report\n * `REDIRECT` when that is not possible. `options.redirectUrl` is a redirect URI the application signs in\n * with, tried before the loopback ones of the settings and the stored client. Never registers a client or\n * prepares an authorization request, so a pending sign-in stays usable. Call while holding the store's\n * refresh lock. Not cancellable: a refresh the server answered may have rotated the refresh token, so its\n * answer must be saved.\n */\nexport async function refreshStoredTokens(\n\tclient: McpOAuthClientOptions,\n\tstate: McpOAuthState,\n\toptions: { fetch?: McpFetch; challenge?: OAuthChallenge; redirectUrl?: string } = {},\n): Promise<OAuthFlowResult> {\n\t// Refreshing never redirects the user; the redirect URI only decides whether tokens without a stored\n\t// client can be refreshed with a Client ID Metadata Document, so any URI the documents list works.\n\tconst candidates = [\n\t\toptions.redirectUrl,\n\t\tloopbackSettings(client.settings).fixedRedirectUrl,\n\t\tregisteredRedirectUrls(state.clientInformation)[0],\n\t].filter((url) => url !== undefined);\n\tconst redirectUrl =\n\t\tcandidates.find((url) => client.clientMetadataDocuments?.listsRedirectUrl(url)) ?? DEFAULT_REDIRECT_URL;\n\treturn authorizeMcp(createOAuthClientProvider(client, redirectUrl), {\n\t\tserverUrl: client.serverUrl,\n\t\tresourceMetadataUrl: options.challenge?.resourceMetadataUrl,\n\t\tauthorizationServerMetadataUrl: client.settings.authServerMetadataUrl,\n\t\tfetch: timedFetch(options.fetch),\n\t\trefreshOnly: true,\n\t});\n}\n"]}
@@ -0,0 +1,48 @@
1
+ export interface OAuthCallback {
2
+ code: string;
3
+ state: string;
4
+ iss?: string;
5
+ }
6
+ /** Outcome shown on the browser page after the redirect. */
7
+ export type OAuthCallbackPage = {
8
+ ok: true;
9
+ } | {
10
+ ok: false;
11
+ message: string;
12
+ details?: string;
13
+ };
14
+ export interface OAuthCallbackServerOptions {
15
+ /** Address to listen on. Default: `127.0.0.1`. */
16
+ host?: string;
17
+ /**
18
+ * Host name in `redirectUrl`, for example `localhost` for a client registered with it while
19
+ * listening on `127.0.0.1`. Default: `host`.
20
+ */
21
+ redirectHost?: string;
22
+ port?: number;
23
+ path?: string;
24
+ /** More paths that receive the callback, for example a server-specific path of a redirect URI. */
25
+ extraPaths?: string[];
26
+ timeoutMs?: number;
27
+ /** Render the browser page as HTML. Default: a plain-text message. */
28
+ renderPage?: (page: OAuthCallbackPage) => string;
29
+ }
30
+ export declare class OAuthCallbackServer {
31
+ readonly redirectUrl: string;
32
+ private server;
33
+ private paths;
34
+ private timeoutMs;
35
+ private renderPage;
36
+ private pending;
37
+ private constructor();
38
+ static listen(options?: OAuthCallbackServerOptions): Promise<OAuthCallbackServer>;
39
+ /**
40
+ * Wait for the authorization response with `state`. With `path`, a response on another path fails, so
41
+ * a server-specific redirect URI can tell authorization servers apart (RFC 9700 section 4.4.2.2).
42
+ */
43
+ waitForCallback(state: string, path?: string): Promise<OAuthCallback>;
44
+ close(): Promise<void>;
45
+ private reply;
46
+ private handle;
47
+ }
48
+ //# sourceMappingURL=callback.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"callback.d.ts","sourceRoot":"","sources":["../../src/oauth/callback.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,aAAa;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,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