@routecraft/ai 0.4.0 → 0.5.0-canary.2
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.
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +234 -14
- package/dist/index.d.ts +234 -14
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -495,17 +495,61 @@ interface AuthPrincipal {
|
|
|
495
495
|
claims?: Record<string, unknown>;
|
|
496
496
|
}
|
|
497
497
|
/**
|
|
498
|
-
*
|
|
499
|
-
*
|
|
498
|
+
* Token info returned by the OAuth `verifyAccessToken` callback.
|
|
499
|
+
* Mirrors the MCP SDK's `AuthInfo` with only the fields routecraft needs.
|
|
500
500
|
*
|
|
501
|
-
*
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
501
|
+
* @experimental
|
|
502
|
+
*/
|
|
503
|
+
interface OAuthTokenInfo {
|
|
504
|
+
/** The raw access token string. */
|
|
505
|
+
token: string;
|
|
506
|
+
/** OAuth client ID that obtained this token. */
|
|
507
|
+
clientId: string;
|
|
508
|
+
/** Scopes granted to the token. */
|
|
509
|
+
scopes: string[];
|
|
510
|
+
/** Expiry as Unix epoch seconds. */
|
|
511
|
+
expiresAt?: number;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* OAuth client info returned by the `getClient` callback.
|
|
515
|
+
* Mirrors the MCP SDK's `OAuthClientInformationFull` with only the fields routecraft needs.
|
|
516
|
+
*
|
|
517
|
+
* Field names use snake_case to match the OAuth 2.0 Dynamic Client Registration
|
|
518
|
+
* specification (RFC 7591) and the MCP SDK's `OAuthClientInformationFull`.
|
|
519
|
+
*
|
|
520
|
+
* @experimental
|
|
521
|
+
*/
|
|
522
|
+
interface OAuthClientInfo {
|
|
523
|
+
/** The client identifier. */
|
|
524
|
+
client_id: string;
|
|
525
|
+
/** Allowed redirect URIs for authorization code flow. */
|
|
526
|
+
redirect_uris: string[];
|
|
527
|
+
/** Human-readable client name. */
|
|
528
|
+
client_name?: string;
|
|
529
|
+
/** Client secret (for confidential clients). */
|
|
530
|
+
client_secret?: string;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Endpoint URLs for the upstream OAuth provider (used by the proxy).
|
|
534
|
+
*
|
|
535
|
+
* @experimental
|
|
536
|
+
*/
|
|
537
|
+
interface OAuthProxyEndpoints {
|
|
538
|
+
/** Authorization endpoint URL. */
|
|
539
|
+
authorizationUrl: string;
|
|
540
|
+
/** Token endpoint URL. */
|
|
541
|
+
tokenUrl: string;
|
|
542
|
+
/** Token revocation endpoint URL. */
|
|
543
|
+
revocationUrl?: string;
|
|
544
|
+
/** Dynamic client registration endpoint URL. */
|
|
545
|
+
registrationUrl?: string;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Validator-based auth: bearer token checked on every request.
|
|
549
|
+
* Used with `jwt()` helper or custom validator functions.
|
|
505
550
|
*
|
|
506
551
|
* @example
|
|
507
552
|
* ```ts
|
|
508
|
-
* // Built-in JWT helper (HMAC / HS256)
|
|
509
553
|
* import { jwt } from "@routecraft/ai";
|
|
510
554
|
* auth: jwt({ secret: process.env.JWT_SECRET! })
|
|
511
555
|
*
|
|
@@ -521,7 +565,7 @@ interface AuthPrincipal {
|
|
|
521
565
|
*
|
|
522
566
|
* @experimental
|
|
523
567
|
*/
|
|
524
|
-
interface
|
|
568
|
+
interface McpValidatorAuthOptions {
|
|
525
569
|
/**
|
|
526
570
|
* Validator function called with the raw bearer token on every request.
|
|
527
571
|
*
|
|
@@ -533,6 +577,70 @@ interface McpHttpAuthOptions {
|
|
|
533
577
|
*/
|
|
534
578
|
validator: McpAuthValidator;
|
|
535
579
|
}
|
|
580
|
+
/**
|
|
581
|
+
* OAuth provider auth: full OAuth 2.1 server flow with proxy to upstream IdP.
|
|
582
|
+
* Mounts discovery, authorization, token, and revocation endpoints alongside `/mcp`.
|
|
583
|
+
* Uses the MCP SDK's `ProxyOAuthServerProvider` and `mcpAuthRouter` internally.
|
|
584
|
+
*
|
|
585
|
+
* @example
|
|
586
|
+
* ```ts
|
|
587
|
+
* import { oauth } from "@routecraft/ai";
|
|
588
|
+
* auth: oauth({
|
|
589
|
+
* issuerUrl: "https://mcp.example.com",
|
|
590
|
+
* endpoints: {
|
|
591
|
+
* authorizationUrl: "https://idp.example.com/authorize",
|
|
592
|
+
* tokenUrl: "https://idp.example.com/token",
|
|
593
|
+
* },
|
|
594
|
+
* verifyAccessToken: async (token) => ({
|
|
595
|
+
* token, clientId: "my-client", scopes: ["read"],
|
|
596
|
+
* }),
|
|
597
|
+
* getClient: async (clientId) => ({
|
|
598
|
+
* client_id: clientId, redirect_uris: ["http://localhost:3000/callback"],
|
|
599
|
+
* }),
|
|
600
|
+
* })
|
|
601
|
+
* ```
|
|
602
|
+
*
|
|
603
|
+
* @experimental
|
|
604
|
+
*/
|
|
605
|
+
interface McpOAuthAuthOptions {
|
|
606
|
+
/** Discriminant for the union. Always `"oauth"`. */
|
|
607
|
+
provider: "oauth";
|
|
608
|
+
/** Issuer URL for OAuth metadata discovery. Must be HTTPS in production. */
|
|
609
|
+
issuerUrl: string | URL;
|
|
610
|
+
/** Base URL for OAuth endpoints (defaults to issuerUrl). */
|
|
611
|
+
baseUrl?: string | URL;
|
|
612
|
+
/** Upstream OAuth provider endpoints to proxy. */
|
|
613
|
+
endpoints: OAuthProxyEndpoints;
|
|
614
|
+
/**
|
|
615
|
+
* Verify an access token and return token info.
|
|
616
|
+
* Called on every authenticated request to `/mcp`.
|
|
617
|
+
*/
|
|
618
|
+
verifyAccessToken: (token: string) => Promise<OAuthTokenInfo>;
|
|
619
|
+
/**
|
|
620
|
+
* Look up a registered OAuth client by ID.
|
|
621
|
+
* Return `undefined` to reject the client.
|
|
622
|
+
*/
|
|
623
|
+
getClient: (clientId: string) => Promise<OAuthClientInfo | undefined>;
|
|
624
|
+
/** OAuth scopes the server advertises as supported. */
|
|
625
|
+
scopesSupported?: string[];
|
|
626
|
+
/** Scopes required on every request to `/mcp`. */
|
|
627
|
+
requiredScopes?: string[];
|
|
628
|
+
/** URL to service documentation (included in OAuth metadata). */
|
|
629
|
+
serviceDocumentationUrl?: string | URL;
|
|
630
|
+
/** Human-readable resource name (included in OAuth metadata). */
|
|
631
|
+
resourceName?: string;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Authentication options for the MCP HTTP server.
|
|
635
|
+
* Only applies when `transport` is `"http"`. Ignored for stdio.
|
|
636
|
+
*
|
|
637
|
+
* Two strategies are supported:
|
|
638
|
+
* - `Validator`: simple bearer token check via `jwt()` or custom function.
|
|
639
|
+
* - `OAuth`: full OAuth 2.1 server flow via `oauth()`, proxying to an upstream IdP.
|
|
640
|
+
*
|
|
641
|
+
* @experimental
|
|
642
|
+
*/
|
|
643
|
+
type McpHttpAuthOptions = McpValidatorAuthOptions | McpOAuthAuthOptions;
|
|
536
644
|
/**
|
|
537
645
|
* A function that validates a bearer token and resolves the authenticated principal.
|
|
538
646
|
* Called on every incoming HTTP request.
|
|
@@ -883,6 +991,77 @@ type JwtAuthOptions = JwtHmacOptions | JwtRsaOptions;
|
|
|
883
991
|
*/
|
|
884
992
|
declare function jwt(options: JwtAuthOptions): McpHttpAuthOptions;
|
|
885
993
|
|
|
994
|
+
/**
|
|
995
|
+
* Options for the `oauth()` factory.
|
|
996
|
+
*
|
|
997
|
+
* @experimental
|
|
998
|
+
*/
|
|
999
|
+
interface OAuthFactoryOptions {
|
|
1000
|
+
/** Issuer URL for OAuth metadata discovery. Must be HTTPS in production. */
|
|
1001
|
+
issuerUrl: string | URL;
|
|
1002
|
+
/** Base URL for OAuth endpoints (defaults to issuerUrl). */
|
|
1003
|
+
baseUrl?: string | URL;
|
|
1004
|
+
/** Upstream OAuth provider endpoints to proxy. */
|
|
1005
|
+
endpoints: OAuthProxyEndpoints;
|
|
1006
|
+
/**
|
|
1007
|
+
* Verify an access token and return token info.
|
|
1008
|
+
* Called on every authenticated request to `/mcp`.
|
|
1009
|
+
*/
|
|
1010
|
+
verifyAccessToken: (token: string) => Promise<OAuthTokenInfo>;
|
|
1011
|
+
/**
|
|
1012
|
+
* Look up a registered OAuth client by ID.
|
|
1013
|
+
* Return `undefined` to reject the client.
|
|
1014
|
+
*/
|
|
1015
|
+
getClient: (clientId: string) => Promise<OAuthClientInfo | undefined>;
|
|
1016
|
+
/** OAuth scopes the server advertises as supported. */
|
|
1017
|
+
scopesSupported?: string[];
|
|
1018
|
+
/** Scopes required on every request to `/mcp`. */
|
|
1019
|
+
requiredScopes?: string[];
|
|
1020
|
+
/** URL to service documentation (included in OAuth metadata). */
|
|
1021
|
+
serviceDocumentationUrl?: string | URL;
|
|
1022
|
+
/** Human-readable resource name (included in OAuth metadata). */
|
|
1023
|
+
resourceName?: string;
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Built-in OAuth authentication helper for MCP HTTP servers.
|
|
1027
|
+
* Configures a full OAuth 2.1 server flow that proxies to an upstream identity
|
|
1028
|
+
* provider using the MCP SDK's `ProxyOAuthServerProvider` and `mcpAuthRouter`.
|
|
1029
|
+
*
|
|
1030
|
+
* Returns an {@link McpHttpAuthOptions} that can be passed directly to
|
|
1031
|
+
* `mcpPlugin({ auth: oauth({ ... }) })`.
|
|
1032
|
+
*
|
|
1033
|
+
* The server will mount OAuth endpoints (`/.well-known/oauth-authorization-server`,
|
|
1034
|
+
* `/authorize`, `/token`, `/revoke`) alongside the `/mcp` transport endpoint.
|
|
1035
|
+
*
|
|
1036
|
+
* @example
|
|
1037
|
+
* ```ts
|
|
1038
|
+
* import { mcpPlugin, oauth } from "@routecraft/ai";
|
|
1039
|
+
*
|
|
1040
|
+
* mcpPlugin({
|
|
1041
|
+
* transport: "http",
|
|
1042
|
+
* auth: oauth({
|
|
1043
|
+
* issuerUrl: "https://mcp.example.com",
|
|
1044
|
+
* endpoints: {
|
|
1045
|
+
* authorizationUrl: "https://idp.example.com/authorize",
|
|
1046
|
+
* tokenUrl: "https://idp.example.com/token",
|
|
1047
|
+
* },
|
|
1048
|
+
* verifyAccessToken: async (token) => ({
|
|
1049
|
+
* token,
|
|
1050
|
+
* clientId: "my-client",
|
|
1051
|
+
* scopes: ["read"],
|
|
1052
|
+
* }),
|
|
1053
|
+
* getClient: async (clientId) => ({
|
|
1054
|
+
* client_id: clientId,
|
|
1055
|
+
* redirect_uris: ["http://localhost:3000/callback"],
|
|
1056
|
+
* }),
|
|
1057
|
+
* }),
|
|
1058
|
+
* });
|
|
1059
|
+
* ```
|
|
1060
|
+
*
|
|
1061
|
+
* @experimental
|
|
1062
|
+
*/
|
|
1063
|
+
declare function oauth(options: OAuthFactoryOptions): McpOAuthAuthOptions;
|
|
1064
|
+
|
|
886
1065
|
/**
|
|
887
1066
|
* MCP plugin: one plugin per adapter. Starts the MCP server during plugin apply (before routes start) so startup failures fail context build. Exposes mcp() routes to external MCP clients.
|
|
888
1067
|
* Optional clients: register named remote MCP servers so routes can use .to(mcp("name:tool")) without passing url.
|
|
@@ -908,7 +1087,10 @@ declare class McpServer {
|
|
|
908
1087
|
private options;
|
|
909
1088
|
private server;
|
|
910
1089
|
private transport;
|
|
911
|
-
/**
|
|
1090
|
+
/**
|
|
1091
|
+
* Node HTTP server when transport is http; used to listen on port and close on stop.
|
|
1092
|
+
* When OAuth is enabled this holds the Express app's underlying server.
|
|
1093
|
+
*/
|
|
912
1094
|
private httpServer;
|
|
913
1095
|
/** Active HTTP sessions keyed by session ID (each session has its own server+transport pair). */
|
|
914
1096
|
private httpSessions;
|
|
@@ -925,18 +1107,56 @@ declare class McpServer {
|
|
|
925
1107
|
private startStdio;
|
|
926
1108
|
/**
|
|
927
1109
|
* Start HTTP transport (streamable-http).
|
|
928
|
-
*
|
|
929
|
-
* Each client session (identified by the `mcp-session-id` header) gets its own
|
|
930
|
-
* Server and StreamableHTTPServerTransport so that reconnecting clients can
|
|
931
|
-
* establish fresh sessions without restarting the process.
|
|
1110
|
+
* Dispatches to the OAuth or raw-HTTP path depending on the auth config.
|
|
932
1111
|
*/
|
|
933
1112
|
private startHttp;
|
|
1113
|
+
/**
|
|
1114
|
+
* Import the MCP SDK Server constructor and StreamableHTTPServerTransport.
|
|
1115
|
+
* Shared by both HTTP startup paths.
|
|
1116
|
+
*/
|
|
1117
|
+
private importSdkHttp;
|
|
1118
|
+
/**
|
|
1119
|
+
* Creates a new MCP Server + Transport pair for a single HTTP session.
|
|
1120
|
+
* Called on every initialization request (no session ID header).
|
|
1121
|
+
*/
|
|
1122
|
+
private createSession;
|
|
1123
|
+
/**
|
|
1124
|
+
* Route an MCP request through session management with principal context.
|
|
1125
|
+
* Shared by both HTTP startup paths.
|
|
1126
|
+
*/
|
|
1127
|
+
private handleMcpRequest;
|
|
1128
|
+
/**
|
|
1129
|
+
* Start HTTP transport with validator-based auth (existing behavior).
|
|
1130
|
+
* Uses raw Node.js `http.createServer`.
|
|
1131
|
+
*/
|
|
1132
|
+
private startHttpWithValidator;
|
|
1133
|
+
/**
|
|
1134
|
+
* Start HTTP transport with OAuth provider auth.
|
|
1135
|
+
* Uses Express to mount `mcpAuthRouter` (OAuth endpoints) alongside `/mcp`.
|
|
1136
|
+
* Express is available as a transitive dependency of `@modelcontextprotocol/sdk`.
|
|
1137
|
+
*
|
|
1138
|
+
* Note: if the server runs behind a reverse proxy, `req.ip` and `req.protocol`
|
|
1139
|
+
* may be incorrect. Users should set `trust proxy` on the Express app via a
|
|
1140
|
+
* future configuration option or by using a custom HTTP server.
|
|
1141
|
+
*/
|
|
1142
|
+
private startHttpWithOAuth;
|
|
1143
|
+
/**
|
|
1144
|
+
* Bind the HTTP server to the configured port and host.
|
|
1145
|
+
* Used by the validator path.
|
|
1146
|
+
*/
|
|
1147
|
+
private listenHttp;
|
|
1148
|
+
/**
|
|
1149
|
+
* Convert the MCP SDK's AuthInfo (set by requireBearerAuth) to an AuthPrincipal
|
|
1150
|
+
* for routecraft's exchange headers.
|
|
1151
|
+
*/
|
|
1152
|
+
private authInfoToPrincipal;
|
|
934
1153
|
/**
|
|
935
1154
|
* When transport is http, returns the bound port (useful when port 0 was used). Otherwise undefined.
|
|
936
1155
|
*/
|
|
937
1156
|
getHttpPort(): number | undefined;
|
|
938
1157
|
/**
|
|
939
1158
|
* Validate the Authorization header using the configured validator.
|
|
1159
|
+
* Only used on the validator auth path (not OAuth -- that uses Express middleware).
|
|
940
1160
|
* Returns the authenticated principal on success, or `null` to reject with 401.
|
|
941
1161
|
*/
|
|
942
1162
|
private validateAuth;
|
|
@@ -1155,4 +1375,4 @@ declare function embeddingPlugin(options?: EmbeddingPluginOptions): CraftPlugin;
|
|
|
1155
1375
|
*/
|
|
1156
1376
|
declare function disposeEmbeddingPipelineCache(): Promise<void>;
|
|
1157
1377
|
|
|
1158
|
-
export { ADAPTER_LLM_OPTIONS, ADAPTER_LLM_PROVIDERS, ADAPTER_MCP_CLIENT_SERVERS, AgentDestinationAdapter, type AgentModelId, type AgentOptions, type AgentPromptSource, type AgentResult, type AuthPrincipal, BRAND, BRAND_MCP_ADAPTER, EmbeddingDestinationAdapter, type EmbeddingModelConfig, type EmbeddingModelConfigHuggingFace, type EmbeddingModelConfigOllama, type EmbeddingModelConfigOpenAI, type EmbeddingModelId, type EmbeddingOptions, type EmbeddingPluginOptions, type EmbeddingPluginProviders, type EmbeddingProviderType, type EmbeddingResult, type JwtAuthOptions, type JwtHmacOptions, type JwtRsaOptions, type LlmAnthropicProviderOptions, LlmDestinationAdapter, type LlmGeminiProviderOptions, type LlmModelConfig, type LlmModelConfigAnthropic, type LlmModelConfigGemini, type LlmModelConfigOllama, type LlmModelConfigOpenAI, type LlmModelConfigOpenRouter, type LlmModelId, type LlmOllamaProviderOptions, type LlmOpenAIProviderOptions, type LlmOpenRouterProviderOptions, type LlmOptions, type LlmPluginOptions, type LlmPluginProviders, type LlmPromptSource, type LlmProviderRegistry, type LlmProviderType, type LlmResult, type LlmUsage, MCP_PLUGIN_REGISTERED, MCP_STDIO_MANAGERS, MCP_TOOL_REGISTRY, type McpArgsExtractor, type McpAuthValidator, type McpClientAuthOptions, type McpClientHttpConfig, type McpClientOptions, type McpClientServerConfig, type McpClientStdioConfig, type McpClientTokenProvider, McpHeadersKeys, type McpHttpAuthOptions, type McpMessage, type McpOptions, type McpPluginOptions, McpServer, type McpServerOptions, type McpServerRegistry, type McpTool, McpToolRegistry, type McpToolRegistryEntry, type McpToolResult, type RegisteredLlmModelId, type RegisteredMcpServer, type RegisteredMcpShorthand, agent, defaultArgs, disposeEmbeddingPipelineCache, embedding, embeddingPlugin, isMcpAdapter, jwt, llm, llmPlugin, mcp, mcpPlugin, validateLlmPluginOptions, validateWithSchema };
|
|
1378
|
+
export { ADAPTER_LLM_OPTIONS, ADAPTER_LLM_PROVIDERS, ADAPTER_MCP_CLIENT_SERVERS, AgentDestinationAdapter, type AgentModelId, type AgentOptions, type AgentPromptSource, type AgentResult, type AuthPrincipal, BRAND, BRAND_MCP_ADAPTER, EmbeddingDestinationAdapter, type EmbeddingModelConfig, type EmbeddingModelConfigHuggingFace, type EmbeddingModelConfigOllama, type EmbeddingModelConfigOpenAI, type EmbeddingModelId, type EmbeddingOptions, type EmbeddingPluginOptions, type EmbeddingPluginProviders, type EmbeddingProviderType, type EmbeddingResult, type JwtAuthOptions, type JwtHmacOptions, type JwtRsaOptions, type LlmAnthropicProviderOptions, LlmDestinationAdapter, type LlmGeminiProviderOptions, type LlmModelConfig, type LlmModelConfigAnthropic, type LlmModelConfigGemini, type LlmModelConfigOllama, type LlmModelConfigOpenAI, type LlmModelConfigOpenRouter, type LlmModelId, type LlmOllamaProviderOptions, type LlmOpenAIProviderOptions, type LlmOpenRouterProviderOptions, type LlmOptions, type LlmPluginOptions, type LlmPluginProviders, type LlmPromptSource, type LlmProviderRegistry, type LlmProviderType, type LlmResult, type LlmUsage, MCP_PLUGIN_REGISTERED, MCP_STDIO_MANAGERS, MCP_TOOL_REGISTRY, type McpArgsExtractor, type McpAuthValidator, type McpClientAuthOptions, type McpClientHttpConfig, type McpClientOptions, type McpClientServerConfig, type McpClientStdioConfig, type McpClientTokenProvider, McpHeadersKeys, type McpHttpAuthOptions, type McpMessage, type McpOAuthAuthOptions, type McpOptions, type McpPluginOptions, McpServer, type McpServerOptions, type McpServerRegistry, type McpTool, McpToolRegistry, type McpToolRegistryEntry, type McpToolResult, type McpValidatorAuthOptions, type OAuthClientInfo, type OAuthFactoryOptions, type OAuthProxyEndpoints, type OAuthTokenInfo, type RegisteredLlmModelId, type RegisteredMcpServer, type RegisteredMcpShorthand, agent, defaultArgs, disposeEmbeddingPipelineCache, embedding, embeddingPlugin, isMcpAdapter, jwt, llm, llmPlugin, mcp, mcpPlugin, oauth, validateLlmPluginOptions, validateWithSchema };
|
package/dist/index.d.ts
CHANGED
|
@@ -495,17 +495,61 @@ interface AuthPrincipal {
|
|
|
495
495
|
claims?: Record<string, unknown>;
|
|
496
496
|
}
|
|
497
497
|
/**
|
|
498
|
-
*
|
|
499
|
-
*
|
|
498
|
+
* Token info returned by the OAuth `verifyAccessToken` callback.
|
|
499
|
+
* Mirrors the MCP SDK's `AuthInfo` with only the fields routecraft needs.
|
|
500
500
|
*
|
|
501
|
-
*
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
501
|
+
* @experimental
|
|
502
|
+
*/
|
|
503
|
+
interface OAuthTokenInfo {
|
|
504
|
+
/** The raw access token string. */
|
|
505
|
+
token: string;
|
|
506
|
+
/** OAuth client ID that obtained this token. */
|
|
507
|
+
clientId: string;
|
|
508
|
+
/** Scopes granted to the token. */
|
|
509
|
+
scopes: string[];
|
|
510
|
+
/** Expiry as Unix epoch seconds. */
|
|
511
|
+
expiresAt?: number;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* OAuth client info returned by the `getClient` callback.
|
|
515
|
+
* Mirrors the MCP SDK's `OAuthClientInformationFull` with only the fields routecraft needs.
|
|
516
|
+
*
|
|
517
|
+
* Field names use snake_case to match the OAuth 2.0 Dynamic Client Registration
|
|
518
|
+
* specification (RFC 7591) and the MCP SDK's `OAuthClientInformationFull`.
|
|
519
|
+
*
|
|
520
|
+
* @experimental
|
|
521
|
+
*/
|
|
522
|
+
interface OAuthClientInfo {
|
|
523
|
+
/** The client identifier. */
|
|
524
|
+
client_id: string;
|
|
525
|
+
/** Allowed redirect URIs for authorization code flow. */
|
|
526
|
+
redirect_uris: string[];
|
|
527
|
+
/** Human-readable client name. */
|
|
528
|
+
client_name?: string;
|
|
529
|
+
/** Client secret (for confidential clients). */
|
|
530
|
+
client_secret?: string;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Endpoint URLs for the upstream OAuth provider (used by the proxy).
|
|
534
|
+
*
|
|
535
|
+
* @experimental
|
|
536
|
+
*/
|
|
537
|
+
interface OAuthProxyEndpoints {
|
|
538
|
+
/** Authorization endpoint URL. */
|
|
539
|
+
authorizationUrl: string;
|
|
540
|
+
/** Token endpoint URL. */
|
|
541
|
+
tokenUrl: string;
|
|
542
|
+
/** Token revocation endpoint URL. */
|
|
543
|
+
revocationUrl?: string;
|
|
544
|
+
/** Dynamic client registration endpoint URL. */
|
|
545
|
+
registrationUrl?: string;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Validator-based auth: bearer token checked on every request.
|
|
549
|
+
* Used with `jwt()` helper or custom validator functions.
|
|
505
550
|
*
|
|
506
551
|
* @example
|
|
507
552
|
* ```ts
|
|
508
|
-
* // Built-in JWT helper (HMAC / HS256)
|
|
509
553
|
* import { jwt } from "@routecraft/ai";
|
|
510
554
|
* auth: jwt({ secret: process.env.JWT_SECRET! })
|
|
511
555
|
*
|
|
@@ -521,7 +565,7 @@ interface AuthPrincipal {
|
|
|
521
565
|
*
|
|
522
566
|
* @experimental
|
|
523
567
|
*/
|
|
524
|
-
interface
|
|
568
|
+
interface McpValidatorAuthOptions {
|
|
525
569
|
/**
|
|
526
570
|
* Validator function called with the raw bearer token on every request.
|
|
527
571
|
*
|
|
@@ -533,6 +577,70 @@ interface McpHttpAuthOptions {
|
|
|
533
577
|
*/
|
|
534
578
|
validator: McpAuthValidator;
|
|
535
579
|
}
|
|
580
|
+
/**
|
|
581
|
+
* OAuth provider auth: full OAuth 2.1 server flow with proxy to upstream IdP.
|
|
582
|
+
* Mounts discovery, authorization, token, and revocation endpoints alongside `/mcp`.
|
|
583
|
+
* Uses the MCP SDK's `ProxyOAuthServerProvider` and `mcpAuthRouter` internally.
|
|
584
|
+
*
|
|
585
|
+
* @example
|
|
586
|
+
* ```ts
|
|
587
|
+
* import { oauth } from "@routecraft/ai";
|
|
588
|
+
* auth: oauth({
|
|
589
|
+
* issuerUrl: "https://mcp.example.com",
|
|
590
|
+
* endpoints: {
|
|
591
|
+
* authorizationUrl: "https://idp.example.com/authorize",
|
|
592
|
+
* tokenUrl: "https://idp.example.com/token",
|
|
593
|
+
* },
|
|
594
|
+
* verifyAccessToken: async (token) => ({
|
|
595
|
+
* token, clientId: "my-client", scopes: ["read"],
|
|
596
|
+
* }),
|
|
597
|
+
* getClient: async (clientId) => ({
|
|
598
|
+
* client_id: clientId, redirect_uris: ["http://localhost:3000/callback"],
|
|
599
|
+
* }),
|
|
600
|
+
* })
|
|
601
|
+
* ```
|
|
602
|
+
*
|
|
603
|
+
* @experimental
|
|
604
|
+
*/
|
|
605
|
+
interface McpOAuthAuthOptions {
|
|
606
|
+
/** Discriminant for the union. Always `"oauth"`. */
|
|
607
|
+
provider: "oauth";
|
|
608
|
+
/** Issuer URL for OAuth metadata discovery. Must be HTTPS in production. */
|
|
609
|
+
issuerUrl: string | URL;
|
|
610
|
+
/** Base URL for OAuth endpoints (defaults to issuerUrl). */
|
|
611
|
+
baseUrl?: string | URL;
|
|
612
|
+
/** Upstream OAuth provider endpoints to proxy. */
|
|
613
|
+
endpoints: OAuthProxyEndpoints;
|
|
614
|
+
/**
|
|
615
|
+
* Verify an access token and return token info.
|
|
616
|
+
* Called on every authenticated request to `/mcp`.
|
|
617
|
+
*/
|
|
618
|
+
verifyAccessToken: (token: string) => Promise<OAuthTokenInfo>;
|
|
619
|
+
/**
|
|
620
|
+
* Look up a registered OAuth client by ID.
|
|
621
|
+
* Return `undefined` to reject the client.
|
|
622
|
+
*/
|
|
623
|
+
getClient: (clientId: string) => Promise<OAuthClientInfo | undefined>;
|
|
624
|
+
/** OAuth scopes the server advertises as supported. */
|
|
625
|
+
scopesSupported?: string[];
|
|
626
|
+
/** Scopes required on every request to `/mcp`. */
|
|
627
|
+
requiredScopes?: string[];
|
|
628
|
+
/** URL to service documentation (included in OAuth metadata). */
|
|
629
|
+
serviceDocumentationUrl?: string | URL;
|
|
630
|
+
/** Human-readable resource name (included in OAuth metadata). */
|
|
631
|
+
resourceName?: string;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Authentication options for the MCP HTTP server.
|
|
635
|
+
* Only applies when `transport` is `"http"`. Ignored for stdio.
|
|
636
|
+
*
|
|
637
|
+
* Two strategies are supported:
|
|
638
|
+
* - `Validator`: simple bearer token check via `jwt()` or custom function.
|
|
639
|
+
* - `OAuth`: full OAuth 2.1 server flow via `oauth()`, proxying to an upstream IdP.
|
|
640
|
+
*
|
|
641
|
+
* @experimental
|
|
642
|
+
*/
|
|
643
|
+
type McpHttpAuthOptions = McpValidatorAuthOptions | McpOAuthAuthOptions;
|
|
536
644
|
/**
|
|
537
645
|
* A function that validates a bearer token and resolves the authenticated principal.
|
|
538
646
|
* Called on every incoming HTTP request.
|
|
@@ -883,6 +991,77 @@ type JwtAuthOptions = JwtHmacOptions | JwtRsaOptions;
|
|
|
883
991
|
*/
|
|
884
992
|
declare function jwt(options: JwtAuthOptions): McpHttpAuthOptions;
|
|
885
993
|
|
|
994
|
+
/**
|
|
995
|
+
* Options for the `oauth()` factory.
|
|
996
|
+
*
|
|
997
|
+
* @experimental
|
|
998
|
+
*/
|
|
999
|
+
interface OAuthFactoryOptions {
|
|
1000
|
+
/** Issuer URL for OAuth metadata discovery. Must be HTTPS in production. */
|
|
1001
|
+
issuerUrl: string | URL;
|
|
1002
|
+
/** Base URL for OAuth endpoints (defaults to issuerUrl). */
|
|
1003
|
+
baseUrl?: string | URL;
|
|
1004
|
+
/** Upstream OAuth provider endpoints to proxy. */
|
|
1005
|
+
endpoints: OAuthProxyEndpoints;
|
|
1006
|
+
/**
|
|
1007
|
+
* Verify an access token and return token info.
|
|
1008
|
+
* Called on every authenticated request to `/mcp`.
|
|
1009
|
+
*/
|
|
1010
|
+
verifyAccessToken: (token: string) => Promise<OAuthTokenInfo>;
|
|
1011
|
+
/**
|
|
1012
|
+
* Look up a registered OAuth client by ID.
|
|
1013
|
+
* Return `undefined` to reject the client.
|
|
1014
|
+
*/
|
|
1015
|
+
getClient: (clientId: string) => Promise<OAuthClientInfo | undefined>;
|
|
1016
|
+
/** OAuth scopes the server advertises as supported. */
|
|
1017
|
+
scopesSupported?: string[];
|
|
1018
|
+
/** Scopes required on every request to `/mcp`. */
|
|
1019
|
+
requiredScopes?: string[];
|
|
1020
|
+
/** URL to service documentation (included in OAuth metadata). */
|
|
1021
|
+
serviceDocumentationUrl?: string | URL;
|
|
1022
|
+
/** Human-readable resource name (included in OAuth metadata). */
|
|
1023
|
+
resourceName?: string;
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Built-in OAuth authentication helper for MCP HTTP servers.
|
|
1027
|
+
* Configures a full OAuth 2.1 server flow that proxies to an upstream identity
|
|
1028
|
+
* provider using the MCP SDK's `ProxyOAuthServerProvider` and `mcpAuthRouter`.
|
|
1029
|
+
*
|
|
1030
|
+
* Returns an {@link McpHttpAuthOptions} that can be passed directly to
|
|
1031
|
+
* `mcpPlugin({ auth: oauth({ ... }) })`.
|
|
1032
|
+
*
|
|
1033
|
+
* The server will mount OAuth endpoints (`/.well-known/oauth-authorization-server`,
|
|
1034
|
+
* `/authorize`, `/token`, `/revoke`) alongside the `/mcp` transport endpoint.
|
|
1035
|
+
*
|
|
1036
|
+
* @example
|
|
1037
|
+
* ```ts
|
|
1038
|
+
* import { mcpPlugin, oauth } from "@routecraft/ai";
|
|
1039
|
+
*
|
|
1040
|
+
* mcpPlugin({
|
|
1041
|
+
* transport: "http",
|
|
1042
|
+
* auth: oauth({
|
|
1043
|
+
* issuerUrl: "https://mcp.example.com",
|
|
1044
|
+
* endpoints: {
|
|
1045
|
+
* authorizationUrl: "https://idp.example.com/authorize",
|
|
1046
|
+
* tokenUrl: "https://idp.example.com/token",
|
|
1047
|
+
* },
|
|
1048
|
+
* verifyAccessToken: async (token) => ({
|
|
1049
|
+
* token,
|
|
1050
|
+
* clientId: "my-client",
|
|
1051
|
+
* scopes: ["read"],
|
|
1052
|
+
* }),
|
|
1053
|
+
* getClient: async (clientId) => ({
|
|
1054
|
+
* client_id: clientId,
|
|
1055
|
+
* redirect_uris: ["http://localhost:3000/callback"],
|
|
1056
|
+
* }),
|
|
1057
|
+
* }),
|
|
1058
|
+
* });
|
|
1059
|
+
* ```
|
|
1060
|
+
*
|
|
1061
|
+
* @experimental
|
|
1062
|
+
*/
|
|
1063
|
+
declare function oauth(options: OAuthFactoryOptions): McpOAuthAuthOptions;
|
|
1064
|
+
|
|
886
1065
|
/**
|
|
887
1066
|
* MCP plugin: one plugin per adapter. Starts the MCP server during plugin apply (before routes start) so startup failures fail context build. Exposes mcp() routes to external MCP clients.
|
|
888
1067
|
* Optional clients: register named remote MCP servers so routes can use .to(mcp("name:tool")) without passing url.
|
|
@@ -908,7 +1087,10 @@ declare class McpServer {
|
|
|
908
1087
|
private options;
|
|
909
1088
|
private server;
|
|
910
1089
|
private transport;
|
|
911
|
-
/**
|
|
1090
|
+
/**
|
|
1091
|
+
* Node HTTP server when transport is http; used to listen on port and close on stop.
|
|
1092
|
+
* When OAuth is enabled this holds the Express app's underlying server.
|
|
1093
|
+
*/
|
|
912
1094
|
private httpServer;
|
|
913
1095
|
/** Active HTTP sessions keyed by session ID (each session has its own server+transport pair). */
|
|
914
1096
|
private httpSessions;
|
|
@@ -925,18 +1107,56 @@ declare class McpServer {
|
|
|
925
1107
|
private startStdio;
|
|
926
1108
|
/**
|
|
927
1109
|
* Start HTTP transport (streamable-http).
|
|
928
|
-
*
|
|
929
|
-
* Each client session (identified by the `mcp-session-id` header) gets its own
|
|
930
|
-
* Server and StreamableHTTPServerTransport so that reconnecting clients can
|
|
931
|
-
* establish fresh sessions without restarting the process.
|
|
1110
|
+
* Dispatches to the OAuth or raw-HTTP path depending on the auth config.
|
|
932
1111
|
*/
|
|
933
1112
|
private startHttp;
|
|
1113
|
+
/**
|
|
1114
|
+
* Import the MCP SDK Server constructor and StreamableHTTPServerTransport.
|
|
1115
|
+
* Shared by both HTTP startup paths.
|
|
1116
|
+
*/
|
|
1117
|
+
private importSdkHttp;
|
|
1118
|
+
/**
|
|
1119
|
+
* Creates a new MCP Server + Transport pair for a single HTTP session.
|
|
1120
|
+
* Called on every initialization request (no session ID header).
|
|
1121
|
+
*/
|
|
1122
|
+
private createSession;
|
|
1123
|
+
/**
|
|
1124
|
+
* Route an MCP request through session management with principal context.
|
|
1125
|
+
* Shared by both HTTP startup paths.
|
|
1126
|
+
*/
|
|
1127
|
+
private handleMcpRequest;
|
|
1128
|
+
/**
|
|
1129
|
+
* Start HTTP transport with validator-based auth (existing behavior).
|
|
1130
|
+
* Uses raw Node.js `http.createServer`.
|
|
1131
|
+
*/
|
|
1132
|
+
private startHttpWithValidator;
|
|
1133
|
+
/**
|
|
1134
|
+
* Start HTTP transport with OAuth provider auth.
|
|
1135
|
+
* Uses Express to mount `mcpAuthRouter` (OAuth endpoints) alongside `/mcp`.
|
|
1136
|
+
* Express is available as a transitive dependency of `@modelcontextprotocol/sdk`.
|
|
1137
|
+
*
|
|
1138
|
+
* Note: if the server runs behind a reverse proxy, `req.ip` and `req.protocol`
|
|
1139
|
+
* may be incorrect. Users should set `trust proxy` on the Express app via a
|
|
1140
|
+
* future configuration option or by using a custom HTTP server.
|
|
1141
|
+
*/
|
|
1142
|
+
private startHttpWithOAuth;
|
|
1143
|
+
/**
|
|
1144
|
+
* Bind the HTTP server to the configured port and host.
|
|
1145
|
+
* Used by the validator path.
|
|
1146
|
+
*/
|
|
1147
|
+
private listenHttp;
|
|
1148
|
+
/**
|
|
1149
|
+
* Convert the MCP SDK's AuthInfo (set by requireBearerAuth) to an AuthPrincipal
|
|
1150
|
+
* for routecraft's exchange headers.
|
|
1151
|
+
*/
|
|
1152
|
+
private authInfoToPrincipal;
|
|
934
1153
|
/**
|
|
935
1154
|
* When transport is http, returns the bound port (useful when port 0 was used). Otherwise undefined.
|
|
936
1155
|
*/
|
|
937
1156
|
getHttpPort(): number | undefined;
|
|
938
1157
|
/**
|
|
939
1158
|
* Validate the Authorization header using the configured validator.
|
|
1159
|
+
* Only used on the validator auth path (not OAuth -- that uses Express middleware).
|
|
940
1160
|
* Returns the authenticated principal on success, or `null` to reject with 401.
|
|
941
1161
|
*/
|
|
942
1162
|
private validateAuth;
|
|
@@ -1155,4 +1375,4 @@ declare function embeddingPlugin(options?: EmbeddingPluginOptions): CraftPlugin;
|
|
|
1155
1375
|
*/
|
|
1156
1376
|
declare function disposeEmbeddingPipelineCache(): Promise<void>;
|
|
1157
1377
|
|
|
1158
|
-
export { ADAPTER_LLM_OPTIONS, ADAPTER_LLM_PROVIDERS, ADAPTER_MCP_CLIENT_SERVERS, AgentDestinationAdapter, type AgentModelId, type AgentOptions, type AgentPromptSource, type AgentResult, type AuthPrincipal, BRAND, BRAND_MCP_ADAPTER, EmbeddingDestinationAdapter, type EmbeddingModelConfig, type EmbeddingModelConfigHuggingFace, type EmbeddingModelConfigOllama, type EmbeddingModelConfigOpenAI, type EmbeddingModelId, type EmbeddingOptions, type EmbeddingPluginOptions, type EmbeddingPluginProviders, type EmbeddingProviderType, type EmbeddingResult, type JwtAuthOptions, type JwtHmacOptions, type JwtRsaOptions, type LlmAnthropicProviderOptions, LlmDestinationAdapter, type LlmGeminiProviderOptions, type LlmModelConfig, type LlmModelConfigAnthropic, type LlmModelConfigGemini, type LlmModelConfigOllama, type LlmModelConfigOpenAI, type LlmModelConfigOpenRouter, type LlmModelId, type LlmOllamaProviderOptions, type LlmOpenAIProviderOptions, type LlmOpenRouterProviderOptions, type LlmOptions, type LlmPluginOptions, type LlmPluginProviders, type LlmPromptSource, type LlmProviderRegistry, type LlmProviderType, type LlmResult, type LlmUsage, MCP_PLUGIN_REGISTERED, MCP_STDIO_MANAGERS, MCP_TOOL_REGISTRY, type McpArgsExtractor, type McpAuthValidator, type McpClientAuthOptions, type McpClientHttpConfig, type McpClientOptions, type McpClientServerConfig, type McpClientStdioConfig, type McpClientTokenProvider, McpHeadersKeys, type McpHttpAuthOptions, type McpMessage, type McpOptions, type McpPluginOptions, McpServer, type McpServerOptions, type McpServerRegistry, type McpTool, McpToolRegistry, type McpToolRegistryEntry, type McpToolResult, type RegisteredLlmModelId, type RegisteredMcpServer, type RegisteredMcpShorthand, agent, defaultArgs, disposeEmbeddingPipelineCache, embedding, embeddingPlugin, isMcpAdapter, jwt, llm, llmPlugin, mcp, mcpPlugin, validateLlmPluginOptions, validateWithSchema };
|
|
1378
|
+
export { ADAPTER_LLM_OPTIONS, ADAPTER_LLM_PROVIDERS, ADAPTER_MCP_CLIENT_SERVERS, AgentDestinationAdapter, type AgentModelId, type AgentOptions, type AgentPromptSource, type AgentResult, type AuthPrincipal, BRAND, BRAND_MCP_ADAPTER, EmbeddingDestinationAdapter, type EmbeddingModelConfig, type EmbeddingModelConfigHuggingFace, type EmbeddingModelConfigOllama, type EmbeddingModelConfigOpenAI, type EmbeddingModelId, type EmbeddingOptions, type EmbeddingPluginOptions, type EmbeddingPluginProviders, type EmbeddingProviderType, type EmbeddingResult, type JwtAuthOptions, type JwtHmacOptions, type JwtRsaOptions, type LlmAnthropicProviderOptions, LlmDestinationAdapter, type LlmGeminiProviderOptions, type LlmModelConfig, type LlmModelConfigAnthropic, type LlmModelConfigGemini, type LlmModelConfigOllama, type LlmModelConfigOpenAI, type LlmModelConfigOpenRouter, type LlmModelId, type LlmOllamaProviderOptions, type LlmOpenAIProviderOptions, type LlmOpenRouterProviderOptions, type LlmOptions, type LlmPluginOptions, type LlmPluginProviders, type LlmPromptSource, type LlmProviderRegistry, type LlmProviderType, type LlmResult, type LlmUsage, MCP_PLUGIN_REGISTERED, MCP_STDIO_MANAGERS, MCP_TOOL_REGISTRY, type McpArgsExtractor, type McpAuthValidator, type McpClientAuthOptions, type McpClientHttpConfig, type McpClientOptions, type McpClientServerConfig, type McpClientStdioConfig, type McpClientTokenProvider, McpHeadersKeys, type McpHttpAuthOptions, type McpMessage, type McpOAuthAuthOptions, type McpOptions, type McpPluginOptions, McpServer, type McpServerOptions, type McpServerRegistry, type McpTool, McpToolRegistry, type McpToolRegistryEntry, type McpToolResult, type McpValidatorAuthOptions, type OAuthClientInfo, type OAuthFactoryOptions, type OAuthProxyEndpoints, type OAuthTokenInfo, type RegisteredLlmModelId, type RegisteredMcpServer, type RegisteredMcpShorthand, agent, defaultArgs, disposeEmbeddingPipelineCache, embedding, embeddingPlugin, isMcpAdapter, jwt, llm, llmPlugin, mcp, mcpPlugin, oauth, validateLlmPluginOptions, validateWithSchema };
|