@tsed/platform-mcp 8.41.3 → 8.43.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.
- package/lib/esm/cli/services/mcpServerConnect.js +17 -1
- package/lib/esm/cli/services/mcpStreamableServer.js +1 -1
- package/lib/esm/common/domain/PlatformTokenVerifier.js +169 -0
- package/lib/esm/common/errors/PlatformInvalidToken.js +11 -0
- package/lib/esm/common/errors/PlatformServerError.js +11 -0
- package/lib/esm/common/index.js +12 -0
- package/lib/esm/common/interfaces/PlatformMcpAuthSettings.js +1 -0
- package/lib/esm/common/interfaces/PlatformMcpPreAuth.js +1 -0
- package/lib/esm/common/interfaces/PlatformMcpUpstreamSettings.js +1 -0
- package/lib/esm/common/services/PlatformMcpGatewayService.js +167 -0
- package/lib/esm/common/utils/attachUpstream.js +140 -0
- package/lib/esm/common/utils/createUpstreamTransport.js +21 -0
- package/lib/esm/common/utils/interpolate.js +24 -0
- package/lib/esm/common/utils/listAll.js +13 -0
- package/lib/esm/common/utils/resolveUpstream.js +35 -0
- package/lib/esm/http/index.js +1 -0
- package/lib/esm/http/services/PlatformMcpAuthService.js +131 -0
- package/lib/esm/http/services/PlatformMcpModule.js +74 -7
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/cli/services/mcpStreamableServer.d.ts +1 -1
- package/lib/types/common/domain/PlatformTokenVerifier.d.ts +59 -0
- package/lib/types/common/errors/PlatformInvalidToken.d.ts +9 -0
- package/lib/types/common/errors/PlatformServerError.d.ts +9 -0
- package/lib/types/common/index.d.ts +12 -0
- package/lib/types/common/interfaces/PlatformMcpAuthSettings.d.ts +87 -0
- package/lib/types/common/interfaces/PlatformMcpPreAuth.d.ts +33 -0
- package/lib/types/common/interfaces/PlatformMcpSettings.d.ts +12 -0
- package/lib/types/common/interfaces/PlatformMcpUpstreamSettings.d.ts +77 -0
- package/lib/types/common/services/PlatformMcpGatewayService.d.ts +66 -0
- package/lib/types/common/utils/attachUpstream.d.ts +14 -0
- package/lib/types/common/utils/createUpstreamTransport.d.ts +10 -0
- package/lib/types/common/utils/interpolate.d.ts +19 -0
- package/lib/types/common/utils/listAll.d.ts +3 -0
- package/lib/types/common/utils/resolveUpstream.d.ts +14 -0
- package/lib/types/http/index.d.ts +1 -0
- package/lib/types/http/services/PlatformMcpAuthService.d.ts +71 -0
- package/lib/types/http/services/PlatformMcpModule.d.ts +9 -2
- package/package.json +12 -8
- package/readme.md +2 -0
- package/src/cli/services/mcpServerConnect.spec.ts +40 -4
- package/src/cli/services/mcpServerConnect.ts +21 -1
- package/src/cli/services/mcpStreamableServer.ts +2 -2
- package/src/common/domain/PlatformTokenVerifier.spec.ts +313 -0
- package/src/common/domain/PlatformTokenVerifier.ts +230 -0
- package/src/common/errors/PlatformInvalidToken.ts +12 -0
- package/src/common/errors/PlatformServerError.ts +12 -0
- package/src/common/index.ts +12 -0
- package/src/common/interfaces/PlatformMcpAuthSettings.ts +88 -0
- package/src/common/interfaces/PlatformMcpPreAuth.ts +37 -0
- package/src/common/interfaces/PlatformMcpSettings.ts +12 -0
- package/src/common/interfaces/PlatformMcpUpstreamSettings.ts +80 -0
- package/src/common/services/PlatformMcpGatewayService.spec.ts +282 -0
- package/src/common/services/PlatformMcpGatewayService.ts +247 -0
- package/src/common/utils/attachUpstream.spec.ts +305 -0
- package/src/common/utils/attachUpstream.ts +207 -0
- package/src/common/utils/createUpstreamTransport.spec.ts +94 -0
- package/src/common/utils/createUpstreamTransport.ts +29 -0
- package/src/common/utils/interpolate.spec.ts +33 -0
- package/src/common/utils/interpolate.ts +28 -0
- package/src/common/utils/listAll.spec.ts +28 -0
- package/src/common/utils/listAll.ts +17 -0
- package/src/common/utils/resolveUpstream.spec.ts +78 -0
- package/src/common/utils/resolveUpstream.ts +44 -0
- package/src/http/index.ts +1 -0
- package/src/http/services/PlatformMcpAuthService.spec.ts +297 -0
- package/src/http/services/PlatformMcpAuthService.ts +165 -0
- package/src/http/services/PlatformMcpModule.spec.ts +120 -0
- package/src/http/services/PlatformMcpModule.ts +97 -10
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
|
-
export declare function mcpStreamableServer(createServer: () => McpServer): Promise<unknown>;
|
|
2
|
+
export declare function mcpStreamableServer(createServer: () => McpServer | Promise<McpServer>): Promise<unknown>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type AuthInfo, type OAuthTokenVerifier } from "@modelcontextprotocol/server";
|
|
2
|
+
import type { PlatformMcpAuthSettings } from "../interfaces/PlatformMcpAuthSettings.js";
|
|
3
|
+
type Claims = Record<string, unknown> & {
|
|
4
|
+
aud?: string | string[];
|
|
5
|
+
iss?: string;
|
|
6
|
+
exp?: number;
|
|
7
|
+
scope?: string;
|
|
8
|
+
client_id?: string;
|
|
9
|
+
azp?: string;
|
|
10
|
+
};
|
|
11
|
+
type OAuth = typeof import("oauth4webapi");
|
|
12
|
+
type AuthorizationServer = import("oauth4webapi").AuthorizationServer;
|
|
13
|
+
/**
|
|
14
|
+
* Access token verifier of a protected MCP endpoint.
|
|
15
|
+
*
|
|
16
|
+
* - `offline`: validates the JWT access token (RFC 9068) against the authorization server JWKS, plus issuer, audience and expiration.
|
|
17
|
+
* - `introspection`: asks the authorization server (RFC 7662) with the endpoint's client credentials.
|
|
18
|
+
* - `auth.verifier`: delegates to the custom verifier, replacing the built-in modes.
|
|
19
|
+
*
|
|
20
|
+
* The built-in modes rely on `oauth4webapi`, imported lazily.
|
|
21
|
+
*
|
|
22
|
+
* @module platform/mcp
|
|
23
|
+
*/
|
|
24
|
+
export declare class PlatformTokenVerifier implements OAuthTokenVerifier {
|
|
25
|
+
protected auth: PlatformMcpAuthSettings;
|
|
26
|
+
protected resource: URL;
|
|
27
|
+
readonly mode: "offline" | "introspection";
|
|
28
|
+
readonly audience: string | false;
|
|
29
|
+
/**
|
|
30
|
+
* Kept as a single instance per endpoint: oauth4webapi caches the JWKS against it.
|
|
31
|
+
*/
|
|
32
|
+
protected authorizationServer?: Promise<AuthorizationServer>;
|
|
33
|
+
protected cache: Map<string, {
|
|
34
|
+
authInfo: AuthInfo;
|
|
35
|
+
until: number;
|
|
36
|
+
}>;
|
|
37
|
+
/**
|
|
38
|
+
* @param auth Auth settings of the endpoint.
|
|
39
|
+
* @param resource Canonical URL of the endpoint, used as the expected audience unless `auth.audience` is set.
|
|
40
|
+
*/
|
|
41
|
+
constructor(auth: PlatformMcpAuthSettings, resource: URL);
|
|
42
|
+
/**
|
|
43
|
+
* Verification mode of an endpoint: explicit `mode`, else introspection when client credentials are configured.
|
|
44
|
+
*/
|
|
45
|
+
static getMode(auth: PlatformMcpAuthSettings): "offline" | "introspection";
|
|
46
|
+
verifyAccessToken(token: string): Promise<AuthInfo>;
|
|
47
|
+
protected verifyWithCustomVerifier(token: string): Promise<AuthInfo>;
|
|
48
|
+
protected verifyWithIntrospection(token: string): Promise<AuthInfo>;
|
|
49
|
+
protected verifyOffline(token: string): Promise<AuthInfo>;
|
|
50
|
+
protected introspect(token: string): Promise<AuthInfo>;
|
|
51
|
+
/**
|
|
52
|
+
* Resolves the authorization server metadata once per endpoint: OIDC discovery, then RFC 8414.
|
|
53
|
+
* Discovery is skipped when the endpoint needed by the verification mode is configured explicitly.
|
|
54
|
+
*/
|
|
55
|
+
protected getAuthorizationServer(oauth: OAuth): Promise<AuthorizationServer>;
|
|
56
|
+
protected toAuthInfo(token: string, claims: Claims): AuthInfo;
|
|
57
|
+
private getRequestOptions;
|
|
58
|
+
}
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { OAuthError } from "@modelcontextprotocol/server";
|
|
2
|
+
/**
|
|
3
|
+
* The presented access token cannot be trusted. Answered as a `401 invalid_token` challenge.
|
|
4
|
+
*
|
|
5
|
+
* @module platform/mcp
|
|
6
|
+
*/
|
|
7
|
+
export declare class PlatformInvalidToken extends OAuthError {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { OAuthError } from "@modelcontextprotocol/server";
|
|
2
|
+
/**
|
|
3
|
+
* The authorization server or the configuration failed, whatever the presented token. Answered as `500 server_error`.
|
|
4
|
+
*
|
|
5
|
+
* @module platform/mcp
|
|
6
|
+
*/
|
|
7
|
+
export declare class PlatformServerError extends OAuthError {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
@@ -5,14 +5,26 @@ export * from "./constants/constants.js";
|
|
|
5
5
|
export * from "./decorators/prompt.js";
|
|
6
6
|
export * from "./decorators/resource.js";
|
|
7
7
|
export * from "./decorators/tool.js";
|
|
8
|
+
export * from "./domain/PlatformTokenVerifier.js";
|
|
9
|
+
export * from "./errors/PlatformInvalidToken.js";
|
|
10
|
+
export * from "./errors/PlatformServerError.js";
|
|
8
11
|
export * from "./fn/definePrompt.js";
|
|
9
12
|
export * from "./fn/defineResource.js";
|
|
10
13
|
export * from "./fn/defineTool.js";
|
|
14
|
+
export * from "./interfaces/PlatformMcpAuthSettings.js";
|
|
15
|
+
export * from "./interfaces/PlatformMcpPreAuth.js";
|
|
11
16
|
export * from "./interfaces/PlatformMcpSettings.js";
|
|
17
|
+
export * from "./interfaces/PlatformMcpUpstreamSettings.js";
|
|
18
|
+
export * from "./services/PlatformMcpGatewayService.js";
|
|
12
19
|
export * from "./utils/asResourceResponse.js";
|
|
13
20
|
export * from "./utils/asToolResponse.js";
|
|
21
|
+
export * from "./utils/attachUpstream.js";
|
|
14
22
|
export * from "./utils/createMcpServer.js";
|
|
23
|
+
export * from "./utils/createUpstreamTransport.js";
|
|
15
24
|
export * from "./utils/fromJsonSchema.js";
|
|
25
|
+
export * from "./utils/interpolate.js";
|
|
26
|
+
export * from "./utils/listAll.js";
|
|
16
27
|
export * from "./utils/resolvePrompts.js";
|
|
17
28
|
export * from "./utils/resolveResources.js";
|
|
18
29
|
export * from "./utils/resolveTools.js";
|
|
30
|
+
export * from "./utils/resolveUpstream.js";
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { OAuthTokenVerifier } from "@modelcontextprotocol/server";
|
|
2
|
+
import type { TokenProvider } from "@tsed/di";
|
|
3
|
+
import type { PlatformMcpPreAuthOption } from "./PlatformMcpPreAuth.js";
|
|
4
|
+
/**
|
|
5
|
+
* OAuth 2.1 resource-server configuration of a Ts.ED MCP endpoint.
|
|
6
|
+
*
|
|
7
|
+
* The endpoint never issues tokens: clients are redirected to the configured authorization server,
|
|
8
|
+
* where registration and consent happen.
|
|
9
|
+
*
|
|
10
|
+
* @module platform/mcp
|
|
11
|
+
*/
|
|
12
|
+
export interface PlatformMcpAuthSettings {
|
|
13
|
+
/**
|
|
14
|
+
* Custom authentication check run before the OAuth verification, for instance an API key.
|
|
15
|
+
* When it returns an identity, the OAuth verification is skipped: both methods are accepted on the endpoint.
|
|
16
|
+
*/
|
|
17
|
+
preAuth?: PlatformMcpPreAuthOption;
|
|
18
|
+
/**
|
|
19
|
+
* Issuer URL of the authorization server (OIDC provider) protecting the endpoint.
|
|
20
|
+
*/
|
|
21
|
+
issuer: string;
|
|
22
|
+
/**
|
|
23
|
+
* How access tokens are verified:
|
|
24
|
+
*
|
|
25
|
+
* - `offline`: the JWT access token (RFC 9068, `typ: at+jwt`) is validated locally against the authorization server JWKS.
|
|
26
|
+
* - `introspection`: the authorization server is asked for each token (RFC 7662). Requires `clientId` and `clientSecret`.
|
|
27
|
+
*
|
|
28
|
+
* Defaults to `introspection` when `clientId` is set, `offline` otherwise.
|
|
29
|
+
*/
|
|
30
|
+
mode?: "offline" | "introspection";
|
|
31
|
+
/**
|
|
32
|
+
* Credentials of the MCP endpoint on the authorization server, used to call the introspection endpoint.
|
|
33
|
+
*/
|
|
34
|
+
clientId?: string;
|
|
35
|
+
clientSecret?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Expected audience of the access tokens. Defaults to `resource`.
|
|
38
|
+
*
|
|
39
|
+
* `false` disables the audience check. It is only accepted by the `introspection` mode and makes the endpoint
|
|
40
|
+
* accept any active token of the issuer, whatever the resource it was issued for.
|
|
41
|
+
*/
|
|
42
|
+
audience?: string | false;
|
|
43
|
+
/**
|
|
44
|
+
* JWKS URL used by the `offline` mode. Discovered from the issuer metadata by default.
|
|
45
|
+
*/
|
|
46
|
+
jwksUri?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Introspection URL used by the `introspection` mode. Discovered from the issuer metadata by default.
|
|
49
|
+
*/
|
|
50
|
+
introspectionEndpoint?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Time in seconds an introspection result is reused. Defaults to `60`; `0` disables the cache.
|
|
53
|
+
*/
|
|
54
|
+
cacheTtl?: number;
|
|
55
|
+
/**
|
|
56
|
+
* Allow a non-HTTPS issuer (other than `localhost`). For local development only.
|
|
57
|
+
*/
|
|
58
|
+
allowInsecureRequests?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Custom access token verifier replacing the built-in modes: an object implementing
|
|
61
|
+
* `verifyAccessToken(token)` or a DI token resolving to one.
|
|
62
|
+
*/
|
|
63
|
+
verifier?: OAuthTokenVerifier | TokenProvider<OAuthTokenVerifier>;
|
|
64
|
+
/**
|
|
65
|
+
* Canonical public URL of the MCP endpoint. It is advertised in the protected resource metadata and in the
|
|
66
|
+
* bearer challenge, and is the expected audience of access tokens unless `audience` is set.
|
|
67
|
+
*
|
|
68
|
+
* It is required and never derived from the incoming request, whose `Host` header is controlled by the caller.
|
|
69
|
+
*/
|
|
70
|
+
resource: string;
|
|
71
|
+
/**
|
|
72
|
+
* Scopes advertised in the protected resource metadata.
|
|
73
|
+
*/
|
|
74
|
+
scopesSupported?: string[];
|
|
75
|
+
/**
|
|
76
|
+
* Scopes a token must carry to reach the endpoint.
|
|
77
|
+
*/
|
|
78
|
+
requiredScopes?: string[];
|
|
79
|
+
/**
|
|
80
|
+
* Human-readable name advertised in the protected resource metadata.
|
|
81
|
+
*/
|
|
82
|
+
resourceName?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Documentation URL advertised in the protected resource metadata.
|
|
85
|
+
*/
|
|
86
|
+
resourceDocumentation?: string;
|
|
87
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { AuthInfo } from "@modelcontextprotocol/server";
|
|
2
|
+
import type { TokenProvider } from "@tsed/di";
|
|
3
|
+
import type { PlatformContext } from "@tsed/platform-http";
|
|
4
|
+
export type PlatformMcpPreAuthResult = AuthInfo | undefined | Promise<AuthInfo | undefined>;
|
|
5
|
+
/**
|
|
6
|
+
* Custom authentication check of an MCP endpoint, run before the OAuth verification.
|
|
7
|
+
*
|
|
8
|
+
* - Return an `AuthInfo` to authenticate the request: the OAuth verification is skipped and the identity is exposed
|
|
9
|
+
* to the handlers.
|
|
10
|
+
* - Return `undefined` when the check does not apply to the request: the OAuth verification runs when the endpoint
|
|
11
|
+
* declares an issuer, and the request is rejected with `401` otherwise.
|
|
12
|
+
* - Throw to reject the request.
|
|
13
|
+
*
|
|
14
|
+
* @module platform/mcp
|
|
15
|
+
*/
|
|
16
|
+
export interface PlatformMcpPreAuth {
|
|
17
|
+
preAuth($ctx: PlatformContext): PlatformMcpPreAuthResult;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Accepted forms of the `auth.preAuth` option: a function, or a DI token resolving to a
|
|
21
|
+
* {@link PlatformMcpPreAuth} provider.
|
|
22
|
+
*
|
|
23
|
+
* @module platform/mcp
|
|
24
|
+
*/
|
|
25
|
+
export type PlatformMcpPreAuthOption = (($ctx: PlatformContext) => PlatformMcpPreAuthResult) | TokenProvider<PlatformMcpPreAuth>;
|
|
26
|
+
/**
|
|
27
|
+
* Auth configuration of an MCP endpoint only protected by a custom check, without OAuth.
|
|
28
|
+
*
|
|
29
|
+
* @module platform/mcp
|
|
30
|
+
*/
|
|
31
|
+
export interface PlatformMcpPreAuthSettings {
|
|
32
|
+
preAuth: PlatformMcpPreAuthOption;
|
|
33
|
+
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { Implementation, ServerOptions } from "@modelcontextprotocol/server";
|
|
2
2
|
import type { StreamableHTTPServerTransportOptions } from "@modelcontextprotocol/node";
|
|
3
3
|
import type { TokenProvider } from "@tsed/di";
|
|
4
|
+
import type { PlatformMcpAuthSettings } from "./PlatformMcpAuthSettings.js";
|
|
5
|
+
import type { PlatformMcpPreAuthSettings } from "./PlatformMcpPreAuth.js";
|
|
6
|
+
import type { PlatformMcpUpstreamSettings } from "./PlatformMcpUpstreamSettings.js";
|
|
4
7
|
/**
|
|
5
8
|
* Configuration fragment accepted under `configuration.mcp` to wire the Platform MCP module.
|
|
6
9
|
*
|
|
@@ -54,6 +57,15 @@ export interface PlatformMcpSettings {
|
|
|
54
57
|
* Optional transport options for the MCP server.
|
|
55
58
|
*/
|
|
56
59
|
transportOptions?: StreamableHTTPServerTransportOptions;
|
|
60
|
+
/**
|
|
61
|
+
* Third-party MCP server exposed through this endpoint (gateway mode).
|
|
62
|
+
*/
|
|
63
|
+
upstream?: PlatformMcpUpstreamSettings;
|
|
64
|
+
/**
|
|
65
|
+
* Protect the HTTP endpoint: with an OAuth authorization server (`issuer`, `resource`, ...), with a custom
|
|
66
|
+
* check (`preAuth`), or with both.
|
|
67
|
+
*/
|
|
68
|
+
auth?: PlatformMcpAuthSettings | PlatformMcpPreAuthSettings;
|
|
57
69
|
}
|
|
58
70
|
declare global {
|
|
59
71
|
namespace TsED {
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Include/exclude filter evaluated on upstream names (tools, prompts) or URIs (resources).
|
|
3
|
+
*
|
|
4
|
+
* @module platform/mcp
|
|
5
|
+
*/
|
|
6
|
+
export interface McpUpstreamFilter {
|
|
7
|
+
include?: (string | RegExp)[];
|
|
8
|
+
exclude?: (string | RegExp)[];
|
|
9
|
+
}
|
|
10
|
+
interface McpUpstreamBaseSettings {
|
|
11
|
+
/**
|
|
12
|
+
* Optional prefix applied to the exposed tool and prompt names. No prefix by default.
|
|
13
|
+
*/
|
|
14
|
+
prefix?: string;
|
|
15
|
+
tools?: McpUpstreamFilter;
|
|
16
|
+
resources?: McpUpstreamFilter;
|
|
17
|
+
prompts?: McpUpstreamFilter;
|
|
18
|
+
/**
|
|
19
|
+
* Bounds of the upstream connection pool. One connection (or process, for stdio) is kept per distinct
|
|
20
|
+
* set of interpolated values.
|
|
21
|
+
*/
|
|
22
|
+
pool?: {
|
|
23
|
+
/**
|
|
24
|
+
* Maximum number of live connections. Defaults to `100`.
|
|
25
|
+
*/
|
|
26
|
+
max?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Idle time in milliseconds before a connection is closed. Defaults to `300000`.
|
|
29
|
+
*/
|
|
30
|
+
idleTimeout?: number;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Upstream reached over Streamable HTTP (`http`) or legacy HTTP+SSE (`sse`).
|
|
35
|
+
*
|
|
36
|
+
* @module platform/mcp
|
|
37
|
+
*/
|
|
38
|
+
export interface McpHttpUpstreamSettings extends McpUpstreamBaseSettings {
|
|
39
|
+
type: "http" | "sse";
|
|
40
|
+
url: string;
|
|
41
|
+
/**
|
|
42
|
+
* Headers sent with every upstream request. Values support the `${OAUTH_TOKEN}`, `${OAUTH_CLIENT_ID}`
|
|
43
|
+
* and `${OAUTH_SCOPES}` placeholders, replaced with the verified identity of the caller.
|
|
44
|
+
*/
|
|
45
|
+
headers?: Record<string, string>;
|
|
46
|
+
/**
|
|
47
|
+
* Extra `fetch` options sent with every upstream request.
|
|
48
|
+
*/
|
|
49
|
+
requestInit?: RequestInit;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Upstream started as a local process speaking MCP over stdio (for instance a server started with `npx`).
|
|
53
|
+
*
|
|
54
|
+
* @module platform/mcp
|
|
55
|
+
*/
|
|
56
|
+
export interface McpStdioUpstreamSettings extends McpUpstreamBaseSettings {
|
|
57
|
+
type: "stdio";
|
|
58
|
+
command: string;
|
|
59
|
+
/**
|
|
60
|
+
* Arguments of the command. Values support the same placeholders as `headers`.
|
|
61
|
+
* One process is started per distinct set of interpolated values.
|
|
62
|
+
*/
|
|
63
|
+
args?: string[];
|
|
64
|
+
/**
|
|
65
|
+
* Environment of the child process, merged over the SDK safe environment subset.
|
|
66
|
+
* Values support the same placeholders as `headers`.
|
|
67
|
+
*/
|
|
68
|
+
env?: Record<string, string>;
|
|
69
|
+
cwd?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Third-party MCP server exposed through a Ts.ED MCP endpoint.
|
|
73
|
+
*
|
|
74
|
+
* @module platform/mcp
|
|
75
|
+
*/
|
|
76
|
+
export type PlatformMcpUpstreamSettings = McpHttpUpstreamSettings | McpStdioUpstreamSettings;
|
|
77
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Client, Prompt, Resource, ResourceTemplateType, Tool } from "@modelcontextprotocol/client";
|
|
2
|
+
import { type OnDestroy } from "@tsed/di";
|
|
3
|
+
import type { PlatformMcpUpstreamSettings } from "../interfaces/PlatformMcpUpstreamSettings.js";
|
|
4
|
+
/**
|
|
5
|
+
* Tools, resources and prompts advertised by an upstream MCP server.
|
|
6
|
+
*
|
|
7
|
+
* @module platform/mcp
|
|
8
|
+
*/
|
|
9
|
+
export interface PlatformMcpUpstreamCatalog {
|
|
10
|
+
tools: Tool[];
|
|
11
|
+
resources: Resource[];
|
|
12
|
+
resourceTemplates: ResourceTemplateType[];
|
|
13
|
+
prompts: Prompt[];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Live connection to an upstream MCP server and its cached catalog.
|
|
17
|
+
*
|
|
18
|
+
* @module platform/mcp
|
|
19
|
+
*/
|
|
20
|
+
export interface PlatformMcpUpstreamConnection {
|
|
21
|
+
client: Client;
|
|
22
|
+
getCatalog(): Promise<PlatformMcpUpstreamCatalog>;
|
|
23
|
+
/**
|
|
24
|
+
* Runs an upstream request, keeping the connection out of the pool eviction while it is pending.
|
|
25
|
+
*/
|
|
26
|
+
run<T>(fn: () => Promise<T>): Promise<T>;
|
|
27
|
+
}
|
|
28
|
+
interface PoolEntry {
|
|
29
|
+
lastUsed: number;
|
|
30
|
+
pending: number;
|
|
31
|
+
connection?: Promise<PlatformMcpUpstreamConnection>;
|
|
32
|
+
client?: Client;
|
|
33
|
+
failures: number;
|
|
34
|
+
retryAt: number;
|
|
35
|
+
error?: unknown;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Owns the MCP clients connected to the upstream servers declared on the MCP endpoints.
|
|
39
|
+
*
|
|
40
|
+
* One connection is kept per upstream and per distinct set of interpolated values, so upstreams with
|
|
41
|
+
* caller-independent credentials share a single connection while forwarded identities get their own.
|
|
42
|
+
*
|
|
43
|
+
* @module platform/mcp
|
|
44
|
+
*/
|
|
45
|
+
export declare class PlatformMcpGatewayService implements OnDestroy {
|
|
46
|
+
protected pools: Map<PlatformMcpUpstreamSettings, Map<string, PoolEntry>>;
|
|
47
|
+
/**
|
|
48
|
+
* Returns the connection matching the resolved upstream definition, connecting lazily.
|
|
49
|
+
*
|
|
50
|
+
* @param upstream Upstream declared in the configuration, identifying the pool.
|
|
51
|
+
* @param resolved Same definition with its placeholders interpolated for the caller.
|
|
52
|
+
*/
|
|
53
|
+
getConnection(upstream: PlatformMcpUpstreamSettings, resolved?: PlatformMcpUpstreamSettings): Promise<PlatformMcpUpstreamConnection>;
|
|
54
|
+
$onDestroy(): Promise<void>;
|
|
55
|
+
protected connect(upstream: PlatformMcpUpstreamSettings, entry: PoolEntry): Promise<PlatformMcpUpstreamConnection>;
|
|
56
|
+
protected loadCatalog(client: Client): Promise<PlatformMcpUpstreamCatalog>;
|
|
57
|
+
protected getPool(upstream: PlatformMcpUpstreamSettings): Map<string, PoolEntry>;
|
|
58
|
+
protected getKey(upstream: PlatformMcpUpstreamSettings): string;
|
|
59
|
+
/**
|
|
60
|
+
* Closes idle connections, then the least recently used ones while the pool is full.
|
|
61
|
+
* Connections serving a request and the requested one are kept, so the pool can temporarily exceed `max`.
|
|
62
|
+
*/
|
|
63
|
+
protected evict(upstream: PlatformMcpUpstreamSettings, pool: Map<string, PoolEntry>, current: PoolEntry, now: number): void;
|
|
64
|
+
protected close(entry: PoolEntry): Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type AuthInfo, type McpServer } from "@modelcontextprotocol/server";
|
|
2
|
+
import type { CreateMcpServerOpts } from "./createMcpServer.js";
|
|
3
|
+
/**
|
|
4
|
+
* Registers the tools, resources and prompts of the endpoint's upstream MCP server on the given server.
|
|
5
|
+
*
|
|
6
|
+
* Local declarations take precedence over upstream entries. An unreachable upstream is logged and
|
|
7
|
+
* skipped so the endpoint keeps serving its local declarations.
|
|
8
|
+
*
|
|
9
|
+
* @param server Server already holding the local declarations.
|
|
10
|
+
* @param settings Resolved settings of the MCP endpoint.
|
|
11
|
+
* @param authInfo Verified identity of the caller, used to interpolate the upstream placeholders.
|
|
12
|
+
* @module platform/mcp
|
|
13
|
+
*/
|
|
14
|
+
export declare function attachUpstream(server: McpServer, settings: CreateMcpServerOpts, authInfo?: AuthInfo): Promise<void>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Transport } from "@modelcontextprotocol/client";
|
|
2
|
+
import type { PlatformMcpUpstreamSettings } from "../interfaces/PlatformMcpUpstreamSettings.js";
|
|
3
|
+
/**
|
|
4
|
+
* Creates the MCP client transport matching a resolved upstream definition.
|
|
5
|
+
*
|
|
6
|
+
* The client package is imported lazily so applications without upstream never load it.
|
|
7
|
+
*
|
|
8
|
+
* @module platform/mcp
|
|
9
|
+
*/
|
|
10
|
+
export declare function createUpstreamTransport(upstream: PlatformMcpUpstreamSettings): Promise<Transport>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replaces the `${NAME}` placeholders of a string with the matching variables.
|
|
3
|
+
*
|
|
4
|
+
* Placeholders without a matching variable are left untouched, and a variable set to `undefined` is replaced
|
|
5
|
+
* with an empty string.
|
|
6
|
+
*
|
|
7
|
+
* @param value String holding the placeholders.
|
|
8
|
+
* @param variables Values indexed by placeholder name.
|
|
9
|
+
* @module platform/mcp
|
|
10
|
+
*/
|
|
11
|
+
export declare function interpolate(value: string, variables: Record<string, string | undefined>): string;
|
|
12
|
+
/**
|
|
13
|
+
* Tells whether a string references at least one of the given placeholder names.
|
|
14
|
+
*
|
|
15
|
+
* @param value String to inspect.
|
|
16
|
+
* @param names Placeholder names to look for.
|
|
17
|
+
* @module platform/mcp
|
|
18
|
+
*/
|
|
19
|
+
export declare function hasPlaceholders(value: string, names: readonly string[]): boolean;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AuthInfo } from "@modelcontextprotocol/server";
|
|
2
|
+
import type { PlatformMcpUpstreamSettings } from "../interfaces/PlatformMcpUpstreamSettings.js";
|
|
3
|
+
/**
|
|
4
|
+
* Tells whether the upstream definition references the identity of the caller.
|
|
5
|
+
*/
|
|
6
|
+
export declare function hasUpstreamPlaceholders(upstream: PlatformMcpUpstreamSettings): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Replaces the `${OAUTH_*}` placeholders of an upstream definition with the verified identity of the caller.
|
|
9
|
+
*
|
|
10
|
+
* Headers are interpolated for `http` and `sse` upstreams, `args` and `env` for `stdio` upstreams.
|
|
11
|
+
*
|
|
12
|
+
* @module platform/mcp
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveUpstream<T extends PlatformMcpUpstreamSettings>(upstream: T, authInfo?: AuthInfo): T;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type AuthInfo } from "@modelcontextprotocol/server";
|
|
2
|
+
import type { PlatformContext } from "@tsed/platform-http";
|
|
3
|
+
import type { PlatformMcpAuthSettings, PlatformMcpPreAuthOption, PlatformMcpPreAuthSettings } from "../../common/index.js";
|
|
4
|
+
import { PlatformTokenVerifier } from "../../common/index.js";
|
|
5
|
+
export declare const PROTECTED_RESOURCE_METADATA_PATH = "/.well-known/oauth-protected-resource";
|
|
6
|
+
/**
|
|
7
|
+
* OAuth resource-server behavior of the protected MCP endpoints: configuration validation,
|
|
8
|
+
* protected resource metadata and bearer token verification.
|
|
9
|
+
*
|
|
10
|
+
* @module platform/mcp
|
|
11
|
+
*/
|
|
12
|
+
export declare class PlatformMcpAuthService {
|
|
13
|
+
protected verifiers: WeakMap<PlatformMcpAuthSettings, PlatformTokenVerifier>;
|
|
14
|
+
/**
|
|
15
|
+
* Tells whether the endpoint is protected by an OAuth authorization server, and not only by a custom check.
|
|
16
|
+
*/
|
|
17
|
+
isOAuth(auth?: PlatformMcpAuthSettings | PlatformMcpPreAuthSettings): auth is PlatformMcpAuthSettings;
|
|
18
|
+
/**
|
|
19
|
+
* Runs the custom authentication check of an endpoint.
|
|
20
|
+
*
|
|
21
|
+
* @returns The identity of the caller, or `undefined` when the check does not apply to the request.
|
|
22
|
+
*/
|
|
23
|
+
preAuth(preAuth: PlatformMcpPreAuthOption, $ctx: PlatformContext): Promise<AuthInfo | undefined>;
|
|
24
|
+
/**
|
|
25
|
+
* Returns the token verifier of an endpoint, created once per `auth` configuration.
|
|
26
|
+
*/
|
|
27
|
+
getVerifier(auth: PlatformMcpAuthSettings, resource: URL): PlatformTokenVerifier;
|
|
28
|
+
/**
|
|
29
|
+
* Verifies the bearer token of the incoming request.
|
|
30
|
+
*
|
|
31
|
+
* @returns The verified identity, or the `401`/`403` challenge to send back to the client.
|
|
32
|
+
*/
|
|
33
|
+
verifyMcpRequest(auth: PlatformMcpAuthSettings, $ctx: PlatformContext): Promise<AuthInfo | Response>;
|
|
34
|
+
/**
|
|
35
|
+
* Rejects an `auth` configuration that cannot work or would be unsafe, before any route is mounted.
|
|
36
|
+
*/
|
|
37
|
+
validate(path: string, auth: PlatformMcpAuthSettings): void;
|
|
38
|
+
/**
|
|
39
|
+
* Builds the OAuth 2.0 Protected Resource Metadata (RFC 9728) advertised for the given resource.
|
|
40
|
+
*
|
|
41
|
+
* @throws When the issuer is not an HTTPS URL (outside localhost) and `allowInsecureRequests` is not set.
|
|
42
|
+
*/
|
|
43
|
+
getProtectedResourceMetadata(auth: PlatformMcpAuthSettings, resource: URL): {
|
|
44
|
+
[x: string]: unknown;
|
|
45
|
+
resource: string;
|
|
46
|
+
authorization_servers?: string[] | undefined;
|
|
47
|
+
jwks_uri?: string | undefined;
|
|
48
|
+
scopes_supported?: string[] | undefined;
|
|
49
|
+
bearer_methods_supported?: string[] | undefined;
|
|
50
|
+
resource_signing_alg_values_supported?: string[] | undefined;
|
|
51
|
+
resource_name?: string | undefined;
|
|
52
|
+
resource_documentation?: string | undefined;
|
|
53
|
+
resource_policy_uri?: string | undefined;
|
|
54
|
+
resource_tos_uri?: string | undefined;
|
|
55
|
+
tls_client_certificate_bound_access_tokens?: boolean | undefined;
|
|
56
|
+
authorization_details_types_supported?: string[] | undefined;
|
|
57
|
+
dpop_signing_alg_values_supported?: string[] | undefined;
|
|
58
|
+
dpop_bound_access_tokens_required?: boolean | undefined;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Path of the RFC 9728 metadata document describing the MCP endpoint mounted on `path`.
|
|
62
|
+
*/
|
|
63
|
+
getProtectedResourceMetadataPath(path: string): string;
|
|
64
|
+
/**
|
|
65
|
+
* Canonical URL of the MCP endpoint, as configured in `auth.resource`.
|
|
66
|
+
*
|
|
67
|
+
* It is used for the metadata document, the bearer challenge and the expected audience, and is never
|
|
68
|
+
* derived from the incoming request.
|
|
69
|
+
*/
|
|
70
|
+
getResourceUrl(auth: PlatformMcpAuthSettings): URL;
|
|
71
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { type OnRoutesInit, PlatformContext, type PlatformRouteDetails } from "@tsed/platform-http";
|
|
1
|
+
import { type OnRoutesInit, type PlatformContext, type PlatformRouteDetails } from "@tsed/platform-http";
|
|
2
2
|
import type { PlatformMcpSettings } from "../../common/index.js";
|
|
3
3
|
import { type CreateMcpServerOpts } from "../../common/utils/createMcpServer.js";
|
|
4
|
+
import { PlatformMcpAuthService } from "./PlatformMcpAuthService.js";
|
|
4
5
|
/**
|
|
5
6
|
* Platform module that mounts the MCP HTTP endpoint and forwards requests to the configured server instance.
|
|
6
7
|
*
|
|
@@ -11,8 +12,14 @@ export declare class PlatformMcpModule implements OnRoutesInit {
|
|
|
11
12
|
protected settings: PlatformMcpSettings | PlatformMcpSettings[];
|
|
12
13
|
protected app: import("@tsed/platform-http").PlatformApplication<TsED.Application>;
|
|
13
14
|
protected mcps: PlatformRouteDetails[];
|
|
15
|
+
protected platformAuthService: PlatformMcpAuthService;
|
|
14
16
|
private loaded;
|
|
15
17
|
$onRoutesInit(): void;
|
|
16
18
|
$logRoutes(routes: PlatformRouteDetails[]): PlatformRouteDetails[];
|
|
17
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Rejects a configuration that cannot work or would be unsafe, before any route is mounted.
|
|
21
|
+
*/
|
|
22
|
+
protected validate(path: string, opts: PlatformMcpSettings): void;
|
|
23
|
+
protected metadata(settings: CreateMcpServerOpts, $ctx: PlatformContext): import("@tsed/platform-http").PlatformResponse<any>;
|
|
24
|
+
protected dispatch(settings: CreateMcpServerOpts, $ctx: PlatformContext): Promise<import("@tsed/platform-http").PlatformResponse<any> | undefined>;
|
|
18
25
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-mcp",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.43.0",
|
|
4
4
|
"description": "Model Context Protocol integration for Ts.ED HTTP adapters.",
|
|
5
5
|
"source": "./src/http/index.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -42,20 +42,24 @@
|
|
|
42
42
|
"test:ci": "vitest run --coverage.thresholds.autoUpdate=true"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
+
"@modelcontextprotocol/client": ">=2.0.0",
|
|
45
46
|
"@modelcontextprotocol/node": ">=2.0.0",
|
|
46
47
|
"@modelcontextprotocol/server": ">=2.0.0",
|
|
47
48
|
"change-case": ">=5.4.4",
|
|
49
|
+
"oauth4webapi": ">=3.0.0",
|
|
48
50
|
"tslib": ">=2.7.0",
|
|
49
51
|
"uuid": ">=11.0.2"
|
|
50
52
|
},
|
|
51
53
|
"devDependencies": {
|
|
52
|
-
"@tsed/barrels": "8.
|
|
53
|
-
"@tsed/core": "8.
|
|
54
|
-
"@tsed/di": "8.
|
|
55
|
-
"@tsed/
|
|
56
|
-
"@tsed/
|
|
57
|
-
"@tsed/
|
|
58
|
-
"@tsed/
|
|
54
|
+
"@tsed/barrels": "8.43.0",
|
|
55
|
+
"@tsed/core": "8.43.0",
|
|
56
|
+
"@tsed/di": "8.43.0",
|
|
57
|
+
"@tsed/exceptions": "8.43.0",
|
|
58
|
+
"@tsed/json-mapper": "8.43.0",
|
|
59
|
+
"@tsed/platform-http": "8.43.0",
|
|
60
|
+
"@tsed/schema": "8.43.0",
|
|
61
|
+
"@tsed/typescript": "8.43.0",
|
|
62
|
+
"jose": "^6.1.3",
|
|
59
63
|
"typescript": "6.0.2",
|
|
60
64
|
"vitest": "4.1.4"
|
|
61
65
|
},
|
package/readme.md
CHANGED
|
@@ -34,6 +34,8 @@
|
|
|
34
34
|
- A `PlatformMcpModule` that mounts a configurable `/mcp` endpoint on top of `@tsed/platform-http`.
|
|
35
35
|
- Functional helpers (`defineTool`, `defineResource`, `definePrompt`) to register MCP handlers via DI tokens.
|
|
36
36
|
- Decorators (`@Tool`, `@Prompt`, `@Resource`) for declarative registration inside services/controllers.
|
|
37
|
+
- A gateway mode (`mcp.upstream`) that exposes a third-party MCP server (Streamable HTTP, SSE or stdio) through the Ts.ED endpoint.
|
|
38
|
+
- OAuth protection per endpoint (`mcp.auth`): protected resource metadata, bearer challenge, and token verification offline or by introspection.
|
|
37
39
|
- Shared utilities (`MCP_SERVER`, transport helpers) that wire the MCP SDK v2 server packages into Ts.ED's dependency injection system.
|
|
38
40
|
|
|
39
41
|
## Installation
|