@silkweave/mcp 3.2.0 → 4.0.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/README.md +1 -1
- package/build/cliProxy.mjs +1 -1
- package/build/{registerTools-DkaraOyx.mjs → filter-Baxr12pu.mjs} +54 -88
- package/build/filter-Baxr12pu.mjs.map +1 -0
- package/build/filter-BuYC4eO9.d.mts +60 -0
- package/build/filter-BuYC4eO9.d.mts.map +1 -0
- package/build/index.d.mts +4 -157
- package/build/index.d.mts.map +1 -1
- package/build/index.mjs +5 -280
- package/build/index.mjs.map +1 -1
- package/build/{result-BN0lxM6Q.d.mts → result-CQT2v6P1.d.mts} +3 -59
- package/build/result-CQT2v6P1.d.mts.map +1 -0
- package/build/{result-CXgeE8ca.mjs → result-uIqPNNxF.mjs} +20 -7
- package/build/result-uIqPNNxF.mjs.map +1 -0
- package/build/server.d.mts +163 -0
- package/build/server.d.mts.map +1 -0
- package/build/server.mjs +339 -0
- package/build/server.mjs.map +1 -0
- package/build/tools.d.mts +2 -1
- package/build/tools.mjs +2 -2
- package/package.json +8 -3
- package/build/registerTools-DkaraOyx.mjs.map +0 -1
- package/build/result-BN0lxM6Q.d.mts.map +0 -1
- package/build/result-CXgeE8ca.mjs.map +0 -1
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { i as rpcInfo, n as FilterRequest, r as filterErrorResponse, t as FilterActions } from "./filter-BuYC4eO9.mjs";
|
|
2
|
+
import { Action, AdapterFactory, OnToolCall, SilkweaveContext, SilkweaveOptions } from "@silkweave/core";
|
|
3
|
+
import { CreateMcpExpressAppOptions } from "@modelcontextprotocol/sdk/server/express.js";
|
|
4
|
+
import { Express, Request, RequestHandler } from "express";
|
|
5
|
+
import { AuthConfig, AuthInfo } from "@silkweave/auth";
|
|
6
|
+
import { CorsOptions, CorsOptions as CorsOptions$1 } from "cors";
|
|
7
|
+
import { Server } from "http";
|
|
8
|
+
|
|
9
|
+
//#region src/adapter/http.d.ts
|
|
10
|
+
interface StartMcpHttpOptions extends CreateMcpExpressAppOptions {
|
|
11
|
+
host: string;
|
|
12
|
+
port: number;
|
|
13
|
+
auth?: AuthConfig;
|
|
14
|
+
/** CORS configuration. `false` to disable, omitted/`true` for permissive defaults, or a `CorsOptions` object. */
|
|
15
|
+
cors?: CorsOptions$1 | boolean;
|
|
16
|
+
/** Mount the `/resource/:id` sideload route. Default `true`. */
|
|
17
|
+
sideloadResources?: boolean;
|
|
18
|
+
/** Directory the sideload route reads from. Default `'resources'`. */
|
|
19
|
+
resourceDir?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Per-request tool filter, applied before `registerTools()` on every
|
|
22
|
+
* `POST /mcp` (the stateless transport recomputes the tool list per request,
|
|
23
|
+
* so permission changes apply on the next `tools/list`). See `FilterActions`
|
|
24
|
+
* for the request stand-in (`headers`/`url`/`method`/`toolName`) and error
|
|
25
|
+
* semantics (a throw surfaces as its `SilkweaveError.statusCode` or 500 -
|
|
26
|
+
* never an empty tool list).
|
|
27
|
+
*/
|
|
28
|
+
filterActions?: FilterActions;
|
|
29
|
+
/** Telemetry hook invoked once per tool call (fire-and-forget). */
|
|
30
|
+
onToolCall?: OnToolCall;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Build a fully-wired Express app that exposes the MCP Streamable HTTP
|
|
34
|
+
* transport (plus OAuth / sideload / well-known routes when configured).
|
|
35
|
+
*
|
|
36
|
+
* Pass the resulting `app` to `app.listen(port, host)` yourself, or use the
|
|
37
|
+
* top-level `startMcpServer()` / `http()` adapter conveniences.
|
|
38
|
+
*/
|
|
39
|
+
declare function buildMcpExpressApp(silkweaveOptions: SilkweaveOptions, context: SilkweaveContext, actions: Action[], options: StartMcpHttpOptions): Express;
|
|
40
|
+
/**
|
|
41
|
+
* Spin up a standalone MCP Streamable HTTP server on `host:port` for the
|
|
42
|
+
* given `actions`. Returns the underlying `Server` so callers can close it.
|
|
43
|
+
*
|
|
44
|
+
* Convenience for use cases that don't go through the `silkweave()` builder.
|
|
45
|
+
*/
|
|
46
|
+
declare function startMcpServer(silkweaveOptions: SilkweaveOptions, actions: Action[], options: StartMcpHttpOptions, context?: SilkweaveContext): Promise<Server>;
|
|
47
|
+
/**
|
|
48
|
+
* Silkweave adapter that owns its own HTTP server. Composes the MCP handler
|
|
49
|
+
* primitives into a fully-wired Express app and listens on `host:port`.
|
|
50
|
+
*/
|
|
51
|
+
declare const http: AdapterFactory<StartMcpHttpOptions>;
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/handlers/auth.d.ts
|
|
54
|
+
/**
|
|
55
|
+
* Key under which `authMiddleware` stashes the resolved `AuthInfo` on the
|
|
56
|
+
* Express request. `mcpTransport` reads it back and forks it into the per-request
|
|
57
|
+
* silkweave context (whose `fork` the tool-call context inherits), so auth
|
|
58
|
+
* reaches tool handlers without `AsyncLocalStorage` - keeping this handler set
|
|
59
|
+
* free of `node:async_hooks` for edge/serverless portability.
|
|
60
|
+
*/
|
|
61
|
+
declare const AUTH_REQUEST_KEY = "__silkweaveAuth";
|
|
62
|
+
/** Read the `AuthInfo` a prior `authMiddleware` attached to the request, if any. */
|
|
63
|
+
declare function authFromRequest(req: Request): AuthInfo | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Express middleware that validates the `Authorization: Bearer …` header via
|
|
66
|
+
* the supplied `AuthConfig`. On success the resolved `AuthInfo` is attached to
|
|
67
|
+
* the request (see `authFromRequest` / `AUTH_REQUEST_KEY`); `mcpTransport` forks
|
|
68
|
+
* it into the silkweave context for the tool call.
|
|
69
|
+
*
|
|
70
|
+
* The middleware should NOT be applied to OAuth-discovery / token routes -
|
|
71
|
+
* compose it only on the routes that require an authenticated caller (the MCP
|
|
72
|
+
* transport itself, sideload, etc.).
|
|
73
|
+
*/
|
|
74
|
+
declare function authMiddleware(auth: AuthConfig, context: SilkweaveContext): RequestHandler;
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/handlers/cors.d.ts
|
|
77
|
+
/** Headers required by the MCP protocol that must always be exposed when CORS is in use. */
|
|
78
|
+
declare const MCP_REQUIRED_HEADERS: string[];
|
|
79
|
+
/**
|
|
80
|
+
* CORS middleware preconfigured to expose the headers MCP clients need
|
|
81
|
+
* (`Last-Event-Id`, `Mcp-Protocol-Version`, `WWW-Authenticate`) on top of any
|
|
82
|
+
* user-supplied options. (Stateless transport - no `Mcp-Session-Id`.)
|
|
83
|
+
*
|
|
84
|
+
* Pass `false` to disable, omit / pass `true` for permissive defaults, or pass
|
|
85
|
+
* a `CorsOptions` object to override.
|
|
86
|
+
*/
|
|
87
|
+
declare function mcpCors(corsConfig?: CorsOptions$1 | boolean): RequestHandler | null;
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/handlers/metadata.d.ts
|
|
90
|
+
/**
|
|
91
|
+
* Handler for `GET /.well-known/oauth-protected-resource` (RFC 9728). Returns
|
|
92
|
+
* the resource server's metadata pointing at the configured authorization
|
|
93
|
+
* servers. Requires `auth.resourceUrl` and a non-empty `auth.authorizationServers`.
|
|
94
|
+
*/
|
|
95
|
+
declare function protectedResourceMetadata(auth: AuthConfig): RequestHandler;
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/handlers/oauth.d.ts
|
|
98
|
+
interface OAuthRouteHandlers {
|
|
99
|
+
/** `GET /.well-known/oauth-authorization-server` - RFC 8414 discovery. */
|
|
100
|
+
wellKnownAuthServer: RequestHandler;
|
|
101
|
+
/** `GET /authorize` - start the OAuth flow. */
|
|
102
|
+
authorize: RequestHandler;
|
|
103
|
+
/** `GET {callbackPath}` - provider callback. */
|
|
104
|
+
callback: RequestHandler;
|
|
105
|
+
/** Path the provider should redirect to (defaults to `/auth/callback`). */
|
|
106
|
+
callbackPath: string;
|
|
107
|
+
/** `POST /token` - exchange code / refresh token. Includes urlencoded body parser. */
|
|
108
|
+
token: RequestHandler[];
|
|
109
|
+
/** `POST /register` - dynamic client registration. Includes JSON body parser. */
|
|
110
|
+
register: RequestHandler[];
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Build the OAuth 2.1 proxy route handlers (authorize, callback, token,
|
|
114
|
+
* register, well-known) backed by the configured `auth.provider`. Returns the
|
|
115
|
+
* handlers as individual `RequestHandler`s so callers can register them
|
|
116
|
+
* wherever they like - under `/mcp`, at the server root, etc.
|
|
117
|
+
*
|
|
118
|
+
* `token` and `register` are returned as middleware arrays because the
|
|
119
|
+
* appropriate body parser must run before the handler. Apply with
|
|
120
|
+
* `app.post(path, ...token)`.
|
|
121
|
+
*/
|
|
122
|
+
declare function oauthRoutes(auth: AuthConfig): OAuthRouteHandlers;
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/handlers/sideload.d.ts
|
|
125
|
+
interface SideloadResourceOptions {
|
|
126
|
+
/** Directory to read sideload resources from. Defaults to `resources/` (cwd-relative). */
|
|
127
|
+
resourceDir?: string;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Handler for `GET /resource/:id` - serves a large MCP response that was
|
|
131
|
+
* sideloaded to disk as a `{id}` payload with a `{id}.json` metadata sidecar.
|
|
132
|
+
*
|
|
133
|
+
* The route param `id` is provided by the host framework (Express, Nest, etc.).
|
|
134
|
+
*/
|
|
135
|
+
declare function sideloadResource(options?: SideloadResourceOptions): RequestHandler;
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/handlers/transport.d.ts
|
|
138
|
+
interface McpTransportHandlers {
|
|
139
|
+
/** `POST /mcp` - handle a single stateless MCP request/response (SSE per call). */
|
|
140
|
+
post: RequestHandler;
|
|
141
|
+
}
|
|
142
|
+
interface McpTransportOptions {
|
|
143
|
+
/**
|
|
144
|
+
* Per-request tool filter, applied before `registerTools()` on every POST.
|
|
145
|
+
* See `FilterActions` for the request stand-in and error semantics.
|
|
146
|
+
*/
|
|
147
|
+
filterActions?: FilterActions;
|
|
148
|
+
/** Telemetry hook invoked once per tool call (fire-and-forget). */
|
|
149
|
+
onToolCall?: OnToolCall;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Build the MCP Streamable HTTP transport route handler.
|
|
153
|
+
*
|
|
154
|
+
* Stateless (per the 2026 spec direction): each `POST /mcp` mints a fresh
|
|
155
|
+
* transport + server with `sessionIdGenerator: undefined`, handles exactly that
|
|
156
|
+
* request (streaming progress over SSE when the call carries a `progressToken`),
|
|
157
|
+
* and tears down on response close. No `Mcp-Session-Id`, no session map, no
|
|
158
|
+
* `GET`/`DELETE` reconnect - any request can hit any instance.
|
|
159
|
+
*/
|
|
160
|
+
declare function mcpTransport(silkweaveOptions: SilkweaveOptions, context: SilkweaveContext, actions: Action[], options?: McpTransportOptions): McpTransportHandlers;
|
|
161
|
+
//#endregion
|
|
162
|
+
export { AUTH_REQUEST_KEY, type CorsOptions, FilterActions, FilterRequest, MCP_REQUIRED_HEADERS, McpTransportHandlers, McpTransportOptions, OAuthRouteHandlers, SideloadResourceOptions, StartMcpHttpOptions, authFromRequest, authMiddleware, buildMcpExpressApp, filterErrorResponse, http, mcpCors, mcpTransport, oauthRoutes, protectedResourceMetadata, rpcInfo, sideloadResource, startMcpServer };
|
|
163
|
+
//# sourceMappingURL=server.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.mts","names":[],"sources":["../src/adapter/http.ts","../src/handlers/auth.ts","../src/handlers/cors.ts","../src/handlers/metadata.ts","../src/handlers/oauth.ts","../src/handlers/sideload.ts","../src/handlers/transport.ts"],"mappings":";;;;;;;;;UAciB,mBAAA,SAA4B,0BAAA;EAC3C,IAAA;EACA,IAAA;EACA,IAAA,GAAO,UAAA;;EAEP,IAAA,GAAO,aAAA;EAAA;EAEP,iBAAA;EAaa;EAXb,WAAA;EATqE;;;;;;;;EAkBrE,aAAA,GAAgB,aAAA;EAXhB;EAaA,UAAA,GAAa,UAAA;AAAA;;;;;;AAUf;;iBAAgB,kBAAA,CACd,gBAAA,EAAkB,gBAAA,EAClB,OAAA,EAAS,gBAAA,EACT,OAAA,EAAS,MAAA,IACT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registerTools, t as filterErrorResponse } from "./filter-Baxr12pu.mjs";
|
|
2
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
+
import { createContext, validateActionDisposition } from "@silkweave/core";
|
|
4
|
+
import { readFile } from "fs/promises";
|
|
5
|
+
import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
|
|
6
|
+
import express from "express";
|
|
7
|
+
import { generateProtectedResourceMetadata, validateToken } from "@silkweave/auth";
|
|
8
|
+
import cors from "cors";
|
|
9
|
+
import { basename, resolve, sep } from "path";
|
|
10
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
11
|
+
//#region src/handlers/auth.ts
|
|
12
|
+
/**
|
|
13
|
+
* Key under which `authMiddleware` stashes the resolved `AuthInfo` on the
|
|
14
|
+
* Express request. `mcpTransport` reads it back and forks it into the per-request
|
|
15
|
+
* silkweave context (whose `fork` the tool-call context inherits), so auth
|
|
16
|
+
* reaches tool handlers without `AsyncLocalStorage` - keeping this handler set
|
|
17
|
+
* free of `node:async_hooks` for edge/serverless portability.
|
|
18
|
+
*/
|
|
19
|
+
const AUTH_REQUEST_KEY = "__silkweaveAuth";
|
|
20
|
+
/** Read the `AuthInfo` a prior `authMiddleware` attached to the request, if any. */
|
|
21
|
+
function authFromRequest(req) {
|
|
22
|
+
return req[AUTH_REQUEST_KEY];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Express middleware that validates the `Authorization: Bearer …` header via
|
|
26
|
+
* the supplied `AuthConfig`. On success the resolved `AuthInfo` is attached to
|
|
27
|
+
* the request (see `authFromRequest` / `AUTH_REQUEST_KEY`); `mcpTransport` forks
|
|
28
|
+
* it into the silkweave context for the tool call.
|
|
29
|
+
*
|
|
30
|
+
* The middleware should NOT be applied to OAuth-discovery / token routes -
|
|
31
|
+
* compose it only on the routes that require an authenticated caller (the MCP
|
|
32
|
+
* transport itself, sideload, etc.).
|
|
33
|
+
*/
|
|
34
|
+
function authMiddleware(auth, context) {
|
|
35
|
+
return async (req, res, next) => {
|
|
36
|
+
const result = await validateToken(req.headers.authorization, auth, context.fork({ request: req }));
|
|
37
|
+
if (result.error) {
|
|
38
|
+
for (const [key, value] of Object.entries(result.error.headers)) res.header(key, value);
|
|
39
|
+
res.status(result.error.statusCode).json(result.error.body);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (result.auth) req[AUTH_REQUEST_KEY] = result.auth;
|
|
43
|
+
next();
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/handlers/cors.ts
|
|
48
|
+
/** Headers required by the MCP protocol that must always be exposed when CORS is in use. */
|
|
49
|
+
const MCP_REQUIRED_HEADERS = [
|
|
50
|
+
"WWW-Authenticate",
|
|
51
|
+
"Last-Event-Id",
|
|
52
|
+
"Mcp-Protocol-Version"
|
|
53
|
+
];
|
|
54
|
+
/**
|
|
55
|
+
* CORS middleware preconfigured to expose the headers MCP clients need
|
|
56
|
+
* (`Last-Event-Id`, `Mcp-Protocol-Version`, `WWW-Authenticate`) on top of any
|
|
57
|
+
* user-supplied options. (Stateless transport - no `Mcp-Session-Id`.)
|
|
58
|
+
*
|
|
59
|
+
* Pass `false` to disable, omit / pass `true` for permissive defaults, or pass
|
|
60
|
+
* a `CorsOptions` object to override.
|
|
61
|
+
*/
|
|
62
|
+
function mcpCors(corsConfig = true) {
|
|
63
|
+
if (corsConfig === false) return null;
|
|
64
|
+
const userConfig = corsConfig === true ? {} : corsConfig;
|
|
65
|
+
const userExposed = userConfig.exposedHeaders;
|
|
66
|
+
const exposedHeaders = [...MCP_REQUIRED_HEADERS, ...Array.isArray(userExposed) ? userExposed : userExposed ? [userExposed] : []];
|
|
67
|
+
return cors({
|
|
68
|
+
origin: "*",
|
|
69
|
+
...userConfig,
|
|
70
|
+
exposedHeaders
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/handlers/metadata.ts
|
|
75
|
+
/**
|
|
76
|
+
* Handler for `GET /.well-known/oauth-protected-resource` (RFC 9728). Returns
|
|
77
|
+
* the resource server's metadata pointing at the configured authorization
|
|
78
|
+
* servers. Requires `auth.resourceUrl` and a non-empty `auth.authorizationServers`.
|
|
79
|
+
*/
|
|
80
|
+
function protectedResourceMetadata(auth) {
|
|
81
|
+
if (!auth.resourceUrl || !auth.authorizationServers?.length) throw new Error("@silkweave/mcp protectedResourceMetadata(): auth.resourceUrl and auth.authorizationServers are required");
|
|
82
|
+
const metadata = generateProtectedResourceMetadata(auth.resourceUrl, auth.authorizationServers, auth.requiredScopes);
|
|
83
|
+
return (_req, res) => {
|
|
84
|
+
res.json(metadata);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/handlers/oauth.ts
|
|
89
|
+
function toOAuthReq(req) {
|
|
90
|
+
return {
|
|
91
|
+
method: req.method,
|
|
92
|
+
url: new URL(req.url, `${req.protocol}://${req.get("host")}`),
|
|
93
|
+
headers: Object.fromEntries(Object.entries(req.headers).map(([k, v]) => [k, Array.isArray(v) ? v[0] : v])),
|
|
94
|
+
body: req.body
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function sendOAuth(res, oauthRes) {
|
|
98
|
+
for (const [key, value] of Object.entries(oauthRes.headers)) res.header(key, value);
|
|
99
|
+
if (oauthRes.body) res.status(oauthRes.status).send(typeof oauthRes.body === "string" ? oauthRes.body : JSON.stringify(oauthRes.body));
|
|
100
|
+
else res.status(oauthRes.status).end();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Build the OAuth 2.1 proxy route handlers (authorize, callback, token,
|
|
104
|
+
* register, well-known) backed by the configured `auth.provider`. Returns the
|
|
105
|
+
* handlers as individual `RequestHandler`s so callers can register them
|
|
106
|
+
* wherever they like - under `/mcp`, at the server root, etc.
|
|
107
|
+
*
|
|
108
|
+
* `token` and `register` are returned as middleware arrays because the
|
|
109
|
+
* appropriate body parser must run before the handler. Apply with
|
|
110
|
+
* `app.post(path, ...token)`.
|
|
111
|
+
*/
|
|
112
|
+
function oauthRoutes(auth) {
|
|
113
|
+
if (!auth.provider) throw new Error("@silkweave/mcp oauthRoutes(): auth.provider is required");
|
|
114
|
+
const provider = auth.provider;
|
|
115
|
+
return {
|
|
116
|
+
callbackPath: auth.callbackPath ?? "/auth/callback",
|
|
117
|
+
wellKnownAuthServer: (_req, res) => {
|
|
118
|
+
sendOAuth(res, provider.metadata());
|
|
119
|
+
},
|
|
120
|
+
authorize: async (req, res) => {
|
|
121
|
+
sendOAuth(res, await provider.authorize(toOAuthReq(req)));
|
|
122
|
+
},
|
|
123
|
+
callback: async (req, res) => {
|
|
124
|
+
sendOAuth(res, await provider.callback(toOAuthReq(req)));
|
|
125
|
+
},
|
|
126
|
+
token: [express.urlencoded({ extended: false }), async (req, res) => {
|
|
127
|
+
sendOAuth(res, await provider.token(toOAuthReq(req)));
|
|
128
|
+
}],
|
|
129
|
+
register: [express.json(), async (req, res) => {
|
|
130
|
+
sendOAuth(res, await provider.register(toOAuthReq(req)));
|
|
131
|
+
}]
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
//#region src/handlers/sideload.ts
|
|
136
|
+
/**
|
|
137
|
+
* Handler for `GET /resource/:id` - serves a large MCP response that was
|
|
138
|
+
* sideloaded to disk as a `{id}` payload with a `{id}.json` metadata sidecar.
|
|
139
|
+
*
|
|
140
|
+
* The route param `id` is provided by the host framework (Express, Nest, etc.).
|
|
141
|
+
*/
|
|
142
|
+
function sideloadResource(options = {}) {
|
|
143
|
+
const { resourceDir = "resources" } = options;
|
|
144
|
+
const baseDir = resolve(resourceDir);
|
|
145
|
+
return async (req, res) => {
|
|
146
|
+
const id = req.params["id"];
|
|
147
|
+
if (!id || typeof id !== "string") throw new Error("Invalid ID");
|
|
148
|
+
const safeId = basename(id);
|
|
149
|
+
const target = resolve(baseDir, safeId);
|
|
150
|
+
if (safeId !== id || target !== baseDir && !target.startsWith(baseDir + sep)) {
|
|
151
|
+
res.status(400).json({ error: "invalid_resource_id" });
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const resourceMeta = JSON.parse(await readFile(`${target}.json`, "utf-8"));
|
|
155
|
+
const buffer = await readFile(target);
|
|
156
|
+
res.status(200);
|
|
157
|
+
res.header("Content-Type", resourceMeta.contentType);
|
|
158
|
+
res.send(buffer);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/handlers/transport.ts
|
|
163
|
+
function createMcpServer(options, actions, context, onToolCall) {
|
|
164
|
+
const server = new McpServer({
|
|
165
|
+
name: options.name,
|
|
166
|
+
description: options.description,
|
|
167
|
+
version: options.version
|
|
168
|
+
}, { capabilities: {
|
|
169
|
+
tools: {},
|
|
170
|
+
logging: {}
|
|
171
|
+
} });
|
|
172
|
+
registerTools(server, actions, context, { onToolCall });
|
|
173
|
+
return server;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Build the MCP Streamable HTTP transport route handler.
|
|
177
|
+
*
|
|
178
|
+
* Stateless (per the 2026 spec direction): each `POST /mcp` mints a fresh
|
|
179
|
+
* transport + server with `sessionIdGenerator: undefined`, handles exactly that
|
|
180
|
+
* request (streaming progress over SSE when the call carries a `progressToken`),
|
|
181
|
+
* and tears down on response close. No `Mcp-Session-Id`, no session map, no
|
|
182
|
+
* `GET`/`DELETE` reconnect - any request can hit any instance.
|
|
183
|
+
*/
|
|
184
|
+
function mcpTransport(silkweaveOptions, context, actions, options = {}) {
|
|
185
|
+
actions.forEach(validateActionDisposition);
|
|
186
|
+
const post = async (req, res) => {
|
|
187
|
+
if (Array.isArray(req.body)) {
|
|
188
|
+
res.status(400).json({
|
|
189
|
+
jsonrpc: "2.0",
|
|
190
|
+
error: {
|
|
191
|
+
code: -32600,
|
|
192
|
+
message: "JSON-RPC batch requests are not supported"
|
|
193
|
+
},
|
|
194
|
+
id: null
|
|
195
|
+
});
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
let active = actions;
|
|
199
|
+
if (options.filterActions) try {
|
|
200
|
+
active = await options.filterActions(actions, {
|
|
201
|
+
headers: req.headers,
|
|
202
|
+
url: req.originalUrl ?? req.url,
|
|
203
|
+
...rpcInfo(req.body)
|
|
204
|
+
});
|
|
205
|
+
} catch (error) {
|
|
206
|
+
const { status, body } = filterErrorResponse(error, req.body);
|
|
207
|
+
res.status(status).json(body);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
|
|
212
|
+
const auth = authFromRequest(req);
|
|
213
|
+
const reqContext = auth ? context.fork({ auth }) : context;
|
|
214
|
+
const server = createMcpServer(silkweaveOptions, active, reqContext, options.onToolCall);
|
|
215
|
+
res.on("close", () => {
|
|
216
|
+
transport.close();
|
|
217
|
+
server.close();
|
|
218
|
+
});
|
|
219
|
+
await server.connect(transport);
|
|
220
|
+
await transport.handleRequest(req, res, req.body);
|
|
221
|
+
} catch (error) {
|
|
222
|
+
console.error("Error handling MCP request:", error);
|
|
223
|
+
if (!res.headersSent) res.status(500).json({
|
|
224
|
+
jsonrpc: "2.0",
|
|
225
|
+
error: {
|
|
226
|
+
code: -32603,
|
|
227
|
+
message: "Internal server error"
|
|
228
|
+
},
|
|
229
|
+
id: null
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
return { post };
|
|
234
|
+
}
|
|
235
|
+
//#endregion
|
|
236
|
+
//#region src/adapter/http.ts
|
|
237
|
+
/**
|
|
238
|
+
* Build a fully-wired Express app that exposes the MCP Streamable HTTP
|
|
239
|
+
* transport (plus OAuth / sideload / well-known routes when configured).
|
|
240
|
+
*
|
|
241
|
+
* Pass the resulting `app` to `app.listen(port, host)` yourself, or use the
|
|
242
|
+
* top-level `startMcpServer()` / `http()` adapter conveniences.
|
|
243
|
+
*/
|
|
244
|
+
function buildMcpExpressApp(silkweaveOptions, context, actions, options) {
|
|
245
|
+
const { host, auth, cors: corsConfig, sideloadResources = true, resourceDir, filterActions, onToolCall, ...mcpAppOptions } = options;
|
|
246
|
+
const app = createMcpExpressApp({
|
|
247
|
+
...mcpAppOptions,
|
|
248
|
+
host
|
|
249
|
+
});
|
|
250
|
+
const corsHandler = mcpCors(corsConfig ?? true);
|
|
251
|
+
if (corsHandler) app.use(corsHandler);
|
|
252
|
+
if (auth?.authorizationServers?.length && auth.resourceUrl) app.get("/.well-known/oauth-protected-resource", protectedResourceMetadata(auth));
|
|
253
|
+
let oauthPaths = /* @__PURE__ */ new Set();
|
|
254
|
+
if (auth?.provider) {
|
|
255
|
+
const oauth = oauthRoutes(auth);
|
|
256
|
+
app.get("/.well-known/oauth-authorization-server", oauth.wellKnownAuthServer);
|
|
257
|
+
app.get("/authorize", oauth.authorize);
|
|
258
|
+
app.get(oauth.callbackPath, oauth.callback);
|
|
259
|
+
app.post("/token", ...oauth.token);
|
|
260
|
+
app.post("/register", ...oauth.register);
|
|
261
|
+
oauthPaths = new Set([
|
|
262
|
+
"/.well-known/oauth-authorization-server",
|
|
263
|
+
"/authorize",
|
|
264
|
+
oauth.callbackPath,
|
|
265
|
+
"/token",
|
|
266
|
+
"/register"
|
|
267
|
+
]);
|
|
268
|
+
}
|
|
269
|
+
if (auth) {
|
|
270
|
+
const guard = authMiddleware(auth, context);
|
|
271
|
+
app.use((req, res, next) => {
|
|
272
|
+
if (req.path.startsWith("/.well-known/") || oauthPaths.has(req.path)) return next();
|
|
273
|
+
return guard(req, res, next);
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
if (sideloadResources) app.get("/resource/:id", sideloadResource({ resourceDir }));
|
|
277
|
+
const transport = mcpTransport(silkweaveOptions, context, actions, {
|
|
278
|
+
filterActions,
|
|
279
|
+
onToolCall
|
|
280
|
+
});
|
|
281
|
+
app.post("/mcp", express.json(), transport.post);
|
|
282
|
+
return app;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Spin up a standalone MCP Streamable HTTP server on `host:port` for the
|
|
286
|
+
* given `actions`. Returns the underlying `Server` so callers can close it.
|
|
287
|
+
*
|
|
288
|
+
* Convenience for use cases that don't go through the `silkweave()` builder.
|
|
289
|
+
*/
|
|
290
|
+
async function startMcpServer(silkweaveOptions, actions, options, context) {
|
|
291
|
+
const app = buildMcpExpressApp(silkweaveOptions, context ?? createContext({ adapter: "http" }), actions, options);
|
|
292
|
+
return new Promise((resolve, reject) => {
|
|
293
|
+
const server = app.listen(options.port, options.host, (error) => {
|
|
294
|
+
if (error) {
|
|
295
|
+
reject(error);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
console.log(`MCP Streamable HTTP Server listening on http://${options.host}:${options.port}/mcp`);
|
|
299
|
+
resolve(server);
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Silkweave adapter that owns its own HTTP server. Composes the MCP handler
|
|
305
|
+
* primitives into a fully-wired Express app and listens on `host:port`.
|
|
306
|
+
*/
|
|
307
|
+
const http = (options) => {
|
|
308
|
+
return (silkweaveOptions, baseContext) => {
|
|
309
|
+
const context = baseContext.fork({ adapter: "http" });
|
|
310
|
+
let httpServer;
|
|
311
|
+
return {
|
|
312
|
+
context,
|
|
313
|
+
start: async (actions) => {
|
|
314
|
+
const app = buildMcpExpressApp(silkweaveOptions, context, actions, options);
|
|
315
|
+
httpServer = await new Promise((resolve, reject) => {
|
|
316
|
+
const s = app.listen(options.port, options.host, (error) => {
|
|
317
|
+
if (error) {
|
|
318
|
+
reject(error);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
console.log(`MCP Streamable HTTP Server listening on http://${options.host}:${options.port}/mcp`);
|
|
322
|
+
resolve(s);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
},
|
|
326
|
+
stop: async () => {
|
|
327
|
+
if (!httpServer) return;
|
|
328
|
+
await new Promise((resolve, reject) => {
|
|
329
|
+
httpServer.close((err) => err ? reject(err) : resolve());
|
|
330
|
+
});
|
|
331
|
+
httpServer = void 0;
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
};
|
|
336
|
+
//#endregion
|
|
337
|
+
export { AUTH_REQUEST_KEY, MCP_REQUIRED_HEADERS, authFromRequest, authMiddleware, buildMcpExpressApp, filterErrorResponse, http, mcpCors, mcpTransport, oauthRoutes, protectedResourceMetadata, rpcInfo, sideloadResource, startMcpServer };
|
|
338
|
+
|
|
339
|
+
//# sourceMappingURL=server.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.mjs","names":[],"sources":["../src/handlers/auth.ts","../src/handlers/cors.ts","../src/handlers/metadata.ts","../src/handlers/oauth.ts","../src/handlers/sideload.ts","../src/handlers/transport.ts","../src/adapter/http.ts"],"sourcesContent":["import { AuthConfig, AuthInfo, validateToken } from '@silkweave/auth'\nimport { SilkweaveContext } from '@silkweave/core'\nimport { type Request, type RequestHandler } from 'express'\n\n/**\n * Key under which `authMiddleware` stashes the resolved `AuthInfo` on the\n * Express request. `mcpTransport` reads it back and forks it into the per-request\n * silkweave context (whose `fork` the tool-call context inherits), so auth\n * reaches tool handlers without `AsyncLocalStorage` - keeping this handler set\n * free of `node:async_hooks` for edge/serverless portability.\n */\nexport const AUTH_REQUEST_KEY = '__silkweaveAuth'\n\n/** Read the `AuthInfo` a prior `authMiddleware` attached to the request, if any. */\nexport function authFromRequest(req: Request): AuthInfo | undefined {\n return (req as Request & { [AUTH_REQUEST_KEY]?: AuthInfo })[AUTH_REQUEST_KEY]\n}\n\n/**\n * Express middleware that validates the `Authorization: Bearer …` header via\n * the supplied `AuthConfig`. On success the resolved `AuthInfo` is attached to\n * the request (see `authFromRequest` / `AUTH_REQUEST_KEY`); `mcpTransport` forks\n * it into the silkweave context for the tool call.\n *\n * The middleware should NOT be applied to OAuth-discovery / token routes -\n * compose it only on the routes that require an authenticated caller (the MCP\n * transport itself, sideload, etc.).\n */\nexport function authMiddleware(auth: AuthConfig, context: SilkweaveContext): RequestHandler {\n return async (req, res, next) => {\n const result = await validateToken(req.headers.authorization, auth, context.fork({ request: req }))\n if (result.error) {\n for (const [key, value] of Object.entries(result.error.headers)) { res.header(key, value) }\n res.status(result.error.statusCode).json(result.error.body)\n return\n }\n if (result.auth) {\n (req as Request & { [AUTH_REQUEST_KEY]?: AuthInfo })[AUTH_REQUEST_KEY] = result.auth\n }\n next()\n }\n}\n","import cors, { CorsOptions } from 'cors'\nimport { type RequestHandler } from 'express'\n\n/** Headers required by the MCP protocol that must always be exposed when CORS is in use. */\nexport const MCP_REQUIRED_HEADERS = ['WWW-Authenticate', 'Last-Event-Id', 'Mcp-Protocol-Version']\n\n/**\n * CORS middleware preconfigured to expose the headers MCP clients need\n * (`Last-Event-Id`, `Mcp-Protocol-Version`, `WWW-Authenticate`) on top of any\n * user-supplied options. (Stateless transport - no `Mcp-Session-Id`.)\n *\n * Pass `false` to disable, omit / pass `true` for permissive defaults, or pass\n * a `CorsOptions` object to override.\n */\nexport function mcpCors(corsConfig: CorsOptions | boolean = true): RequestHandler | null {\n if (corsConfig === false) { return null }\n const userConfig = corsConfig === true ? {} : corsConfig\n const userExposed = userConfig.exposedHeaders\n const exposedHeaders = [\n ...MCP_REQUIRED_HEADERS,\n ...(Array.isArray(userExposed) ? userExposed : userExposed ? [userExposed] : [])\n ]\n return cors({ origin: '*', ...userConfig, exposedHeaders })\n}\n","import { AuthConfig, generateProtectedResourceMetadata } from '@silkweave/auth'\nimport { type RequestHandler } from 'express'\n\n/**\n * Handler for `GET /.well-known/oauth-protected-resource` (RFC 9728). Returns\n * the resource server's metadata pointing at the configured authorization\n * servers. Requires `auth.resourceUrl` and a non-empty `auth.authorizationServers`.\n */\nexport function protectedResourceMetadata(auth: AuthConfig): RequestHandler {\n if (!auth.resourceUrl || !auth.authorizationServers?.length) {\n throw new Error('@silkweave/mcp protectedResourceMetadata(): auth.resourceUrl and auth.authorizationServers are required')\n }\n const metadata = generateProtectedResourceMetadata(auth.resourceUrl, auth.authorizationServers, auth.requiredScopes)\n return (_req, res) => { res.json(metadata) }\n}\n","import { AuthConfig, OAuthRequest, OAuthResponse } from '@silkweave/auth'\nimport express, { type Request, type RequestHandler, type Response } from 'express'\n\nfunction toOAuthReq(req: Request): OAuthRequest {\n return {\n method: req.method,\n url: new URL(req.url, `${req.protocol}://${req.get('host')}`),\n headers: Object.fromEntries(Object.entries(req.headers).map(([k, v]) => [k, Array.isArray(v) ? v[0] : v])),\n body: req.body as Record<string, string> | undefined\n }\n}\n\nfunction sendOAuth(res: Response, oauthRes: OAuthResponse) {\n for (const [key, value] of Object.entries(oauthRes.headers)) { res.header(key, value) }\n if (oauthRes.body) {\n res.status(oauthRes.status).send(typeof oauthRes.body === 'string' ? oauthRes.body : JSON.stringify(oauthRes.body))\n } else {\n res.status(oauthRes.status).end()\n }\n}\n\nexport interface OAuthRouteHandlers {\n /** `GET /.well-known/oauth-authorization-server` - RFC 8414 discovery. */\n wellKnownAuthServer: RequestHandler\n /** `GET /authorize` - start the OAuth flow. */\n authorize: RequestHandler\n /** `GET {callbackPath}` - provider callback. */\n callback: RequestHandler\n /** Path the provider should redirect to (defaults to `/auth/callback`). */\n callbackPath: string\n /** `POST /token` - exchange code / refresh token. Includes urlencoded body parser. */\n token: RequestHandler[]\n /** `POST /register` - dynamic client registration. Includes JSON body parser. */\n register: RequestHandler[]\n}\n\n/**\n * Build the OAuth 2.1 proxy route handlers (authorize, callback, token,\n * register, well-known) backed by the configured `auth.provider`. Returns the\n * handlers as individual `RequestHandler`s so callers can register them\n * wherever they like - under `/mcp`, at the server root, etc.\n *\n * `token` and `register` are returned as middleware arrays because the\n * appropriate body parser must run before the handler. Apply with\n * `app.post(path, ...token)`.\n */\nexport function oauthRoutes(auth: AuthConfig): OAuthRouteHandlers {\n if (!auth.provider) { throw new Error('@silkweave/mcp oauthRoutes(): auth.provider is required') }\n const provider = auth.provider\n const callbackPath = auth.callbackPath ?? '/auth/callback'\n\n return {\n callbackPath,\n wellKnownAuthServer: (_req, res) => { sendOAuth(res, provider.metadata()) },\n authorize: async (req, res) => { sendOAuth(res, await provider.authorize(toOAuthReq(req))) },\n callback: async (req, res) => { sendOAuth(res, await provider.callback(toOAuthReq(req))) },\n token: [\n express.urlencoded({ extended: false }),\n async (req, res) => { sendOAuth(res, await provider.token(toOAuthReq(req))) }\n ],\n register: [\n express.json(),\n async (req, res) => { sendOAuth(res, await provider.register(toOAuthReq(req))) }\n ]\n }\n}\n","import { type RequestHandler } from 'express'\nimport { readFile } from 'fs/promises'\nimport { basename, resolve, sep } from 'path'\nimport { type SideloadResource } from '../util/sideload.js'\n\nexport interface SideloadResourceOptions {\n /** Directory to read sideload resources from. Defaults to `resources/` (cwd-relative). */\n resourceDir?: string\n}\n\n/**\n * Handler for `GET /resource/:id` - serves a large MCP response that was\n * sideloaded to disk as a `{id}` payload with a `{id}.json` metadata sidecar.\n *\n * The route param `id` is provided by the host framework (Express, Nest, etc.).\n */\nexport function sideloadResource(options: SideloadResourceOptions = {}): RequestHandler {\n const { resourceDir = 'resources' } = options\n const baseDir = resolve(resourceDir)\n return async (req, res) => {\n const id = req.params['id']\n if (!id || typeof id !== 'string') { throw new Error('Invalid ID') }\n // Contain the read to resourceDir. Express 5 decodes %2F in the route param,\n // so an untrusted `id` like `../../etc/passwd` would otherwise escape the\n // directory. basename() strips any path separators; the resolve+prefix check\n // is defense in depth against platform-specific separator handling.\n const safeId = basename(id)\n const target = resolve(baseDir, safeId)\n if (safeId !== id || (target !== baseDir && !target.startsWith(baseDir + sep))) {\n res.status(400).json({ error: 'invalid_resource_id' })\n return\n }\n const resourceMeta: SideloadResource = JSON.parse(await readFile(`${target}.json`, 'utf-8'))\n const buffer = await readFile(target)\n res.status(200)\n res.header('Content-Type', resourceMeta.contentType)\n res.send(buffer)\n }\n}\n","import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js'\nimport { Action, OnToolCall, SilkweaveContext, SilkweaveOptions, validateActionDisposition } from '@silkweave/core'\nimport { type RequestHandler } from 'express'\nimport { authFromRequest } from './auth.js'\nimport { filterErrorResponse, rpcInfo, type FilterActions } from './filter.js'\nimport { registerTools } from './registerTools.js'\n\nfunction createMcpServer(options: SilkweaveOptions, actions: Action[], context: SilkweaveContext, onToolCall?: OnToolCall): McpServer {\n const server = new McpServer({\n name: options.name,\n description: options.description,\n version: options.version\n }, {\n capabilities: { tools: {}, logging: {} }\n })\n registerTools(server, actions, context, { onToolCall })\n return server\n}\n\nexport interface McpTransportHandlers {\n /** `POST /mcp` - handle a single stateless MCP request/response (SSE per call). */\n post: RequestHandler\n}\n\nexport interface McpTransportOptions {\n /**\n * Per-request tool filter, applied before `registerTools()` on every POST.\n * See `FilterActions` for the request stand-in and error semantics.\n */\n filterActions?: FilterActions\n /** Telemetry hook invoked once per tool call (fire-and-forget). */\n onToolCall?: OnToolCall\n}\n\n/**\n * Build the MCP Streamable HTTP transport route handler.\n *\n * Stateless (per the 2026 spec direction): each `POST /mcp` mints a fresh\n * transport + server with `sessionIdGenerator: undefined`, handles exactly that\n * request (streaming progress over SSE when the call carries a `progressToken`),\n * and tears down on response close. No `Mcp-Session-Id`, no session map, no\n * `GET`/`DELETE` reconnect - any request can hit any instance.\n */\nexport function mcpTransport(\n silkweaveOptions: SilkweaveOptions,\n context: SilkweaveContext,\n actions: Action[],\n options: McpTransportOptions = {}\n): McpTransportHandlers {\n // Fail at boot, not per request - the factory runs once when the app is built.\n actions.forEach(validateActionDisposition)\n\n const post: RequestHandler = async (req, res) => {\n // JSON-RPC batching was removed from the MCP spec (2025-06-18). A batch also\n // defeats per-request filterActions: rpcInfo reflects only the first message\n // but the SDK transport would execute every entry, so a later batch entry\n // could invoke a tool the filter gated on the first. Reject batches outright.\n if (Array.isArray(req.body)) {\n res.status(400).json({ jsonrpc: '2.0', error: { code: -32_600, message: 'JSON-RPC batch requests are not supported' }, id: null })\n return\n }\n let active = actions\n if (options.filterActions) {\n try {\n active = await options.filterActions(actions, { headers: req.headers, url: req.originalUrl ?? req.url, ...rpcInfo(req.body) })\n } catch (error) {\n // A throw never degrades to an empty tool list - it surfaces as its\n // statusCode (SilkweaveError) or a 500, so a bad key reads as an auth\n // failure rather than \"server has no tools\".\n const { status, body } = filterErrorResponse(error, req.body)\n res.status(status).json(body)\n return\n }\n }\n try {\n const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined })\n // Fork the caller's resolved auth (attached by authMiddleware) into the\n // per-request context; registerTools' tool-call fork inherits it.\n const auth = authFromRequest(req)\n const reqContext = auth ? context.fork({ auth }) : context\n const server = createMcpServer(silkweaveOptions, active, reqContext, options.onToolCall)\n res.on('close', () => { void transport.close(); void server.close() })\n await server.connect(transport)\n await transport.handleRequest(req, res, req.body)\n } catch (error) {\n console.error('Error handling MCP request:', error)\n if (!res.headersSent) {\n res.status(500).json({ jsonrpc: '2.0', error: { code: -32_603, message: 'Internal server error' }, id: null })\n }\n }\n }\n\n return { post }\n}\n","import { createMcpExpressApp, type CreateMcpExpressAppOptions } from '@modelcontextprotocol/sdk/server/express.js'\nimport { AuthConfig } from '@silkweave/auth'\nimport { Action, AdapterFactory, createContext, OnToolCall, SilkweaveContext, SilkweaveOptions } from '@silkweave/core'\nimport { CorsOptions } from 'cors'\nimport express, { type Express } from 'express'\nimport { Server } from 'http'\nimport { authMiddleware } from '../handlers/auth.js'\nimport { mcpCors } from '../handlers/cors.js'\nimport { protectedResourceMetadata } from '../handlers/metadata.js'\nimport { oauthRoutes } from '../handlers/oauth.js'\nimport { type FilterActions } from '../handlers/filter.js'\nimport { sideloadResource } from '../handlers/sideload.js'\nimport { mcpTransport } from '../handlers/transport.js'\n\nexport interface StartMcpHttpOptions extends CreateMcpExpressAppOptions {\n host: string\n port: number\n auth?: AuthConfig\n /** CORS configuration. `false` to disable, omitted/`true` for permissive defaults, or a `CorsOptions` object. */\n cors?: CorsOptions | boolean\n /** Mount the `/resource/:id` sideload route. Default `true`. */\n sideloadResources?: boolean\n /** Directory the sideload route reads from. Default `'resources'`. */\n resourceDir?: string\n /**\n * Per-request tool filter, applied before `registerTools()` on every\n * `POST /mcp` (the stateless transport recomputes the tool list per request,\n * so permission changes apply on the next `tools/list`). See `FilterActions`\n * for the request stand-in (`headers`/`url`/`method`/`toolName`) and error\n * semantics (a throw surfaces as its `SilkweaveError.statusCode` or 500 -\n * never an empty tool list).\n */\n filterActions?: FilterActions\n /** Telemetry hook invoked once per tool call (fire-and-forget). */\n onToolCall?: OnToolCall\n}\n\n/**\n * Build a fully-wired Express app that exposes the MCP Streamable HTTP\n * transport (plus OAuth / sideload / well-known routes when configured).\n *\n * Pass the resulting `app` to `app.listen(port, host)` yourself, or use the\n * top-level `startMcpServer()` / `http()` adapter conveniences.\n */\nexport function buildMcpExpressApp(\n silkweaveOptions: SilkweaveOptions,\n context: SilkweaveContext,\n actions: Action[],\n options: StartMcpHttpOptions\n): Express {\n const { host, auth, cors: corsConfig, sideloadResources = true, resourceDir, filterActions, onToolCall, ...mcpAppOptions } = options\n const app = createMcpExpressApp({ ...mcpAppOptions, host })\n\n const corsHandler = mcpCors(corsConfig ?? true)\n if (corsHandler) { app.use(corsHandler) }\n\n if (auth?.authorizationServers?.length && auth.resourceUrl) {\n app.get('/.well-known/oauth-protected-resource', protectedResourceMetadata(auth))\n }\n\n let oauthPaths = new Set<string>()\n if (auth?.provider) {\n const oauth = oauthRoutes(auth)\n app.get('/.well-known/oauth-authorization-server', oauth.wellKnownAuthServer)\n app.get('/authorize', oauth.authorize)\n app.get(oauth.callbackPath, oauth.callback)\n app.post('/token', ...oauth.token)\n app.post('/register', ...oauth.register)\n oauthPaths = new Set(['/.well-known/oauth-authorization-server', '/authorize', oauth.callbackPath, '/token', '/register'])\n }\n\n if (auth) {\n const guard = authMiddleware(auth, context)\n app.use((req, res, next) => {\n if (req.path.startsWith('/.well-known/') || oauthPaths.has(req.path)) { return next() }\n return guard(req, res, next)\n })\n }\n\n if (sideloadResources) {\n app.get('/resource/:id', sideloadResource({ resourceDir }))\n }\n\n const transport = mcpTransport(silkweaveOptions, context, actions, { filterActions, onToolCall })\n app.post('/mcp', express.json(), transport.post)\n\n return app\n}\n\n/**\n * Spin up a standalone MCP Streamable HTTP server on `host:port` for the\n * given `actions`. Returns the underlying `Server` so callers can close it.\n *\n * Convenience for use cases that don't go through the `silkweave()` builder.\n */\nexport async function startMcpServer(\n silkweaveOptions: SilkweaveOptions,\n actions: Action[],\n options: StartMcpHttpOptions,\n context?: SilkweaveContext\n): Promise<Server> {\n const ctx = context ?? createContext({ adapter: 'http' })\n const app = buildMcpExpressApp(silkweaveOptions, ctx, actions, options)\n return new Promise<Server>((resolve, reject) => {\n const server = app.listen(options.port, options.host, (error) => {\n if (error) { reject(error); return }\n console.log(`MCP Streamable HTTP Server listening on http://${options.host}:${options.port}/mcp`)\n resolve(server)\n })\n })\n}\n\n/**\n * Silkweave adapter that owns its own HTTP server. Composes the MCP handler\n * primitives into a fully-wired Express app and listens on `host:port`.\n */\nexport const http: AdapterFactory<StartMcpHttpOptions> = (options) => {\n return (silkweaveOptions, baseContext) => {\n const context = baseContext.fork({ adapter: 'http' })\n let httpServer: Server | undefined\n return {\n context,\n start: async (actions) => {\n const app = buildMcpExpressApp(silkweaveOptions, context, actions, options)\n httpServer = await new Promise<Server>((resolve, reject) => {\n const s = app.listen(options.port, options.host, (error) => {\n if (error) { reject(error); return }\n console.log(`MCP Streamable HTTP Server listening on http://${options.host}:${options.port}/mcp`)\n resolve(s)\n })\n })\n },\n stop: async () => {\n if (!httpServer) { return }\n await new Promise<void>((resolve, reject) => {\n httpServer!.close((err) => err ? reject(err) : resolve())\n })\n httpServer = undefined\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAWA,MAAa,mBAAmB;;AAGhC,SAAgB,gBAAgB,KAAoC;A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,CAAC;EACrD,IAAI;AACJ,SAAO;GACL;GACA,OAAO,OAAO,YAAY;IACxB,MAAM,MAAM,mBAAmB,kBAAkB,SAAS,SAAS,QAAQ;AAC3E,iBAAa,MAAM,IAAI,SAAiB,SAAS,WAAW;KAC1D,MAAM,IAAI,IAAI,OAAO,QAAQ,MAAM,QAAQ,OAAO,UAAU;AAC1D,UAAI,OAAO;AAAE,cAAO,MAAM;AAAE;;AAC5B,cAAQ,IAAI,kDAAkD,QAAQ,KAAK,GAAG,QAAQ,KAAK,MAAM;AACjG,cAAQ,EAAE;OACV;MACF;;GAEJ,MAAM,YAAY;AAChB,QAAI,CAAC,WAAc;AACnB,UAAM,IAAI,SAAe,SAAS,WAAW;AAC3C,gBAAY,OAAO,QAAQ,MAAM,OAAO,IAAI,GAAG,SAAS,CAAC;MACzD;AACF,iBAAa,KAAA;;GAEhB"}
|
package/build/tools.d.mts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as rpcInfo, n as FilterRequest, r as filterErrorResponse, t as FilterActions } from "./filter-BuYC4eO9.mjs";
|
|
2
|
+
import { a as smartToolResult, c as registerTools, i as parseResourceMessage, l as requestFromExtra, n as handleToolError, o as structuredToolResult, r as jsonToolResult, s as RegisterToolsOptions, t as errorToolResult } from "./result-CQT2v6P1.mjs";
|
|
2
3
|
export { FilterActions, FilterRequest, RegisterToolsOptions, errorToolResult, filterErrorResponse, handleToolError, jsonToolResult, parseResourceMessage, registerTools, requestFromExtra, rpcInfo, smartToolResult, structuredToolResult };
|
package/build/tools.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { i as
|
|
2
|
-
import {
|
|
1
|
+
import { a as smartToolResult, i as parseResourceMessage, n as handleToolError, o as structuredToolResult, r as jsonToolResult, t as errorToolResult } from "./result-uIqPNNxF.mjs";
|
|
2
|
+
import { i as requestFromExtra, n as rpcInfo, r as registerTools, t as filterErrorResponse } from "./filter-Baxr12pu.mjs";
|
|
3
3
|
export { errorToolResult, filterErrorResponse, handleToolError, jsonToolResult, parseResourceMessage, registerTools, requestFromExtra, rpcInfo, smartToolResult, structuredToolResult };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@silkweave/mcp",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Silkweave MCP Package",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://www.silkweave.dev",
|
|
@@ -24,6 +24,11 @@
|
|
|
24
24
|
"types": "./build/index.d.mts",
|
|
25
25
|
"default": "./build/index.mjs"
|
|
26
26
|
},
|
|
27
|
+
"./server": {
|
|
28
|
+
"@silkweave/source": "./src/server.ts",
|
|
29
|
+
"types": "./build/server.d.mts",
|
|
30
|
+
"default": "./build/server.mjs"
|
|
31
|
+
},
|
|
27
32
|
"./tools": {
|
|
28
33
|
"@silkweave/source": "./src/tools.ts",
|
|
29
34
|
"types": "./build/tools.d.mts",
|
|
@@ -39,8 +44,8 @@
|
|
|
39
44
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
40
45
|
"change-case": "^5.4.4",
|
|
41
46
|
"zod": "^3.25.0",
|
|
42
|
-
"@silkweave/auth": "
|
|
43
|
-
"@silkweave/core": "
|
|
47
|
+
"@silkweave/auth": "4.0.0",
|
|
48
|
+
"@silkweave/core": "4.0.0"
|
|
44
49
|
},
|
|
45
50
|
"peerDependencies": {
|
|
46
51
|
"commander": "^13.1.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"registerTools-DkaraOyx.mjs","names":[],"sources":["../src/handlers/auth.ts","../src/handlers/filter.ts","../src/handlers/registerTools.ts"],"sourcesContent":["import { AuthConfig, AuthInfo, validateToken } from '@silkweave/auth'\nimport { SilkweaveContext } from '@silkweave/core'\nimport { type RequestHandler } from 'express'\nimport { AsyncLocalStorage } from 'node:async_hooks'\n\n/**\n * Per-request bearer-token storage used by tool handlers to read the resolved\n * `AuthInfo` for the currently-handled MCP call.\n */\nexport const authStorage = new AsyncLocalStorage<AuthInfo>()\n\n/**\n * Express middleware that validates the `Authorization: Bearer …` header via\n * the supplied `AuthConfig`. On success the resolved `AuthInfo` is placed in\n * `authStorage` for the duration of the downstream handler - `mcpTransport`'s\n * tool callbacks pick it up to populate the silkweave context's `auth` key.\n *\n * The middleware should NOT be applied to OAuth-discovery / token routes -\n * compose it only on the routes that require an authenticated caller (the MCP\n * transport itself, sideload, etc.).\n */\nexport function authMiddleware(auth: AuthConfig, context: SilkweaveContext): RequestHandler {\n return async (req, res, next) => {\n const result = await validateToken(req.headers.authorization, auth, context.fork({ request: req }))\n if (result.error) {\n for (const [key, value] of Object.entries(result.error.headers)) { res.header(key, value) }\n res.status(result.error.statusCode).json(result.error.body)\n return\n }\n if (result.auth) {\n authStorage.run(result.auth, () => { next() })\n } else {\n next()\n }\n }\n}\n","import { Action, SilkweaveError } from '@silkweave/core'\n\n/**\n * Normalized request stand-in handed to `filterActions` - the same shape for\n * the Express `http()` transport and the Web-Standard `edge()` adapter.\n */\nexport interface FilterRequest {\n /** Inbound HTTP headers (lower-cased keys, as delivered by the host). */\n headers: Record<string, string | string[] | undefined>\n /** Full request URL (or path, as delivered by the host). */\n url: string\n /**\n * JSON-RPC method of the POSTed message (`'initialize'`, `'tools/list'`,\n * `'tools/call'`, `'ping'`, ...). Lets the callback skip expensive\n * permission lookups on `initialize`/`ping`, and doubles as an\n * observability tap (e.g. counting `tools/list`). For a legacy JSON-RPC\n * batch this is the first request's method. Empty string when the body is\n * not a recognizable JSON-RPC message (the transport will reject it anyway).\n */\n method: string\n /** `params.name` of a `tools/call` message; unset for every other method. */\n toolName?: string\n}\n\n/**\n * Per-request tool filter for the stateless MCP transports. Runs before\n * `registerTools()` on every `POST /mcp`; only the returned actions exist for\n * that request (`tools/list` and `tools/call` alike - a client that cached a\n * wider list is still denied). May be async (e.g. a DB lookup of API-key\n * permissions).\n *\n * Error semantics: a thrown `SilkweaveError` propagates as its `statusCode`\n * (e.g. 401 invalid key, 403 insufficient permissions) with a JSON-RPC error\n * body; any other throw maps to HTTP 500. A thrown error NEVER degrades to an\n * empty tool list - return `[]` explicitly if \"no tools\" is the intended\n * answer.\n */\nexport type FilterActions = (actions: Action[], request: FilterRequest) => Action[] | Promise<Action[]>\n\n/**\n * Extract the JSON-RPC `method` (and `toolName` for `tools/call`) from a\n * parsed request body. Tolerates a legacy batch (first request wins) and\n * unrecognizable bodies (empty `method`).\n */\nexport function rpcInfo(body: unknown): { method: string; toolName?: string } {\n const message = (Array.isArray(body) ? body[0] : body) as { method?: unknown; params?: { name?: unknown } } | undefined\n const method = typeof message?.method === 'string' ? message.method : ''\n const toolName = method === 'tools/call' && typeof message?.params?.name === 'string' ? message.params.name : undefined\n return { method, ...(toolName !== undefined ? { toolName } : {}) }\n}\n\n/**\n * Map a `filterActions` throw to an HTTP status + JSON-RPC error body: a\n * `SilkweaveError` keeps its `statusCode` (so an invalid key surfaces as an\n * auth failure, not \"server has no tools\"), anything else is a 500. `id` is\n * echoed from the request body when available.\n */\nexport function filterErrorResponse(error: unknown, body: unknown): { status: number; body: object } {\n const message = (Array.isArray(body) ? body[0] : body) as { id?: unknown } | undefined\n const id = message?.id ?? null\n if (error instanceof SilkweaveError) {\n return {\n status: error.statusCode,\n body: { jsonrpc: '2.0', error: { code: -32_000, message: error.message }, id }\n }\n }\n console.error('filterActions error:', error)\n return {\n status: 500,\n body: { jsonrpc: '2.0', error: { code: -32_603, message: 'Internal server error' }, id }\n }\n}\n","import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js'\nimport type { ServerNotification, ServerRequest } from '@modelcontextprotocol/sdk/types.js'\nimport { Action, ActionRun, createLogger, emitToolCall, isStreamingAction, OnToolCall, runStreamingAction, SilkweaveContext, SilkweaveError, ToolCallEvent } from '@silkweave/core'\nimport { capitalCase, pascalCase } from 'change-case'\nimport { errorToolResult, handleToolError, jsonToolResult, smartToolResult, structuredToolResult } from '../util/result.js'\nimport { authStorage } from './auth.js'\n\ntype LogStream = NonNullable<Parameters<typeof createLogger>[0]>['stream']\ntype ToolExtra = RequestHandlerExtra<ServerRequest, ServerNotification>\n\nexport interface RegisterToolsOptions {\n /**\n * Where the per-call logger writes its human-readable stream. The `stdio`\n * adapter MUST pass `false` (stdout is the MCP protocol channel); the HTTP\n * transports default to `process.stderr`.\n */\n logStream?: LogStream\n /**\n * Telemetry hook invoked once per tool call (fire-and-forget; errors are\n * logged, never propagated). Fires after result formatting, so events carry\n * `resultBytes` (serialized raw-result size) and `sideloaded` (whether\n * `smartToolResult` offloaded to an embedded resource).\n */\n onToolCall?: OnToolCall\n}\n\n/**\n * Build the generic `request` context value (the same key REST/tRPC populate)\n * from the MCP SDK's `extra.requestInfo`, so transport-agnostic consumers - e.g.\n * `@silkweave/nestjs` `@UseGuards` guards reading\n * `switchToHttp().getRequest().headers` - work over MCP. There are no path\n * `params`/`query`/`body` on the raw MCP call, so those start as empty\n * stand-ins; `@silkweave/nestjs` later fills them from the validated tool input\n * per the reflected param sources (path -> `params`, `@Query` -> `query`,\n * `@Body` -> `body`) so guards reading any of them decide as they would over\n * REST. Returns `undefined` when no HTTP request info is available.\n */\nexport function requestFromExtra(requestInfo: { headers?: unknown; url?: { toString(): string } } | undefined) {\n if (!requestInfo) { return undefined }\n return { headers: requestInfo.headers ?? {}, url: requestInfo.url?.toString(), params: {}, query: {}, body: {} }\n}\n\n/** Per-call logger that bridges silkweave logs/progress onto MCP notifications. */\nfunction createToolLogger(extra: ToolExtra, stream: LogStream) {\n return createLogger({\n stream,\n onLog: (level, data) => {\n extra.sendNotification({ method: 'notifications/message', params: { level, data } })\n },\n onProgress: ({ progress, total, message }) => {\n if (!extra._meta?.progressToken) { return }\n extra.sendNotification({\n method: 'notifications/progress',\n params: { progress, total, message, progressToken: extra._meta.progressToken }\n })\n }\n })\n}\n\n/** Run the action, streaming chunks as progress notifications when a token is set. */\nasync function runAction(action: Action, input: object, context: SilkweaveContext, extra: ToolExtra): Promise<object | object[]> {\n const progressToken = extra._meta?.progressToken\n if (isStreamingAction(action)) {\n return runStreamingAction(action, input, context, progressToken\n ? async (chunk, index) => {\n await extra.sendNotification({\n method: 'notifications/progress',\n params: { progressToken, progress: index + 1, message: JSON.stringify(chunk) }\n })\n }\n : undefined)\n }\n return (action.run as ActionRun<object, object>)(input, context)\n}\n\n/**\n * Result path for a `disposition: 'structured'` action: parse the raw result\n * through the output schema and ship the PARSED data as `structuredContent`.\n * Parsing strips extra fields (so the client-side JSON-Schema validator, which\n * rejects `additionalProperties`, passes by construction) and a genuine\n * mismatch (missing required field, wrong type) degrades to an `isError` tool\n * result - which the SDK exempts from output validation - instead of an opaque\n * protocol error.\n */\nfunction structuredResult(action: Action, result: object | object[]) {\n const parsed = action.output!.safeParse(result)\n if (!parsed.success) {\n const fields = [...new Set(parsed.error.issues.map((issue) => issue.path.join('.') || '(root)'))].join(', ')\n return errorToolResult(new SilkweaveError(\n `Output validation failed for '${action.name}' at: ${fields}. The tool returned a shape that does not match its declared output schema - this is a server-side bug, not an input problem; retrying with different arguments will not help.`,\n 'output_validation_error'\n ))\n }\n return structuredToolResult(parsed.data as object)\n}\n\n/** MCP-only telemetry fields, computed only when a hook is registered. */\nfunction resultMeta(result: object | object[], formatted: { content?: { type: string }[] }): Pick<ToolCallEvent, 'resultBytes' | 'sideloaded'> {\n return {\n resultBytes: JSON.stringify(result).length,\n sideloaded: (formatted.content ?? []).some((block) => block.type === 'resource')\n }\n}\n\n/** Error identity for telemetry: a SilkweaveError's `code`, else the error's name. */\nfunction errorMeta(error: unknown): Pick<ToolCallEvent, 'errorCode' | 'errorMessage'> {\n if (error instanceof SilkweaveError) { return { errorCode: error.code, errorMessage: error.message } }\n if (error instanceof Error) { return { errorCode: error.name, errorMessage: error.message } }\n return { errorCode: 'unknown' }\n}\n\n/** Format via the action's `toolResult` hook, else the resolved disposition. */\nfunction formatToolResult(action: Action, result: object | object[], context: SilkweaveContext, disposition: unknown) {\n if (action.toolResult) {\n const response = action.toolResult(result, context)\n if (response) { return response }\n }\n // A structured action's output schema is a contract fixed at tools/list\n // time - a client's `_meta.disposition` cannot demote it.\n if (action.disposition === 'structured') { return structuredResult(action, result) }\n return disposition === 'smart' ? smartToolResult(result) : jsonToolResult(result)\n}\n\n/**\n * Register every action as an MCP tool on `server`. Shared by all MCP transports\n * (`stdio`, `http`, `edge`). Each tool call forks `context` with a per-call\n * `logger`, the SDK `extra`, a synthesized `request` (see `requestFromExtra`),\n * and - when bearer auth ran for this request - the resolved `auth`. The result\n * is formatted by the action's `toolResult` hook if present, otherwise per the\n * resolved disposition (client `_meta.disposition` > `action.disposition` >\n * `json`); a `'structured'` action always ships schema-parsed\n * `structuredContent` (its contract cannot be demoted per-call).\n */\nexport function registerTools(\n server: McpServer,\n actions: Action[],\n context: SilkweaveContext,\n options: RegisterToolsOptions = {}\n) {\n const stream = options.logStream ?? process.stderr\n for (const action of actions) {\n server.registerTool(pascalCase(action.name), {\n title: capitalCase(action.name),\n description: action.description,\n inputSchema: action.input,\n // Derived base (query ⇒ read-only), explicit annotations merged over.\n annotations: { readOnlyHint: action.kind === 'query', ...action.annotations },\n // Only structured actions declare an outputSchema contract - the SDK\n // enforces it server-side and SDK clients enforce it independently, so\n // forwarding is strictly opt-in via disposition.\n ...(action.disposition === 'structured' && action.output ? { outputSchema: action.output } : {})\n }, async (input, extra) => {\n const logger = createToolLogger(extra, stream)\n const currentAuth = authStorage.getStore()\n const actionContext = context.fork({\n logger,\n extra,\n request: requestFromExtra(extra.requestInfo),\n ...(currentAuth ? { auth: currentAuth } : {})\n })\n // Client-sent `_meta.disposition` wins; otherwise fall back to the\n // action's configured default (`json` when neither is set).\n const disposition = extra._meta?.disposition ?? action.disposition\n const base = { action: action.name, tool: pascalCase(action.name), transport: 'mcp' as const, context: actionContext }\n const started = Date.now()\n try {\n const result = await runAction(action, input, actionContext, extra)\n const formatted = formatToolResult(action, result, actionContext, disposition)\n emitToolCall(options.onToolCall, {\n ...base,\n durationMs: Date.now() - started,\n ok: formatted.isError !== true,\n ...(options.onToolCall ? resultMeta(result, formatted) : {})\n })\n return formatted\n } catch (error) {\n emitToolCall(options.onToolCall, { ...base, durationMs: Date.now() - started, ok: false, ...errorMeta(error) })\n return handleToolError(error)\n }\n })\n }\n}\n"],"mappings":";;;;;;;;;;AASA,MAAa,cAAc,IAAI,mBAA6B;;;;;;;;;;;AAY5D,SAAgB,eAAe,MAAkB,SAA2C;AAC1F,QAAO,OAAO,KAAK,KAAK,SAAS;EAC/B,MAAM,SAAS,MAAM,cAAc,IAAI,QAAQ,eAAe,MAAM,QAAQ,KAAK,EAAE,SAAS,KAAK,CAAC,CAAC;AACnG,MAAI,OAAO,OAAO;AAChB,QAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,MAAM,QAAQ,CAAI,KAAI,OAAO,KAAK,MAAM;AACzF,OAAI,OAAO,OAAO,MAAM,WAAW,CAAC,KAAK,OAAO,MAAM,KAAK;AAC3D;;AAEF,MAAI,OAAO,KACT,aAAY,IAAI,OAAO,YAAY;AAAE,SAAM;IAAG;MAE9C,OAAM;;;;;;;;;;ACYZ,SAAgB,QAAQ,MAAsD;CAC5E,MAAM,UAAW,MAAM,QAAQ,KAAK,GAAG,KAAK,KAAK;CACjD,MAAM,SAAS,OAAO,SAAS,WAAW,WAAW,QAAQ,SAAS;CACtE,MAAM,WAAW,WAAW,gBAAgB,OAAO,SAAS,QAAQ,SAAS,WAAW,QAAQ,OAAO,OAAO,KAAA;AAC9G,QAAO;EAAE;EAAQ,GAAI,aAAa,KAAA,IAAY,EAAE,UAAU,GAAG,EAAE;EAAG;;;;;;;;AASpE,SAAgB,oBAAoB,OAAgB,MAAiD;CAEnG,MAAM,MADW,MAAM,QAAQ,KAAK,GAAG,KAAK,KAAK,OAC7B,MAAM;AAC1B,KAAI,iBAAiB,eACnB,QAAO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