@silkweave/mcp 3.1.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,6 +59,52 @@ Exposes a single stateless `POST /mcp` (each call mints a fresh transport with `
59
59
  | `port` | `number` | Listen port |
60
60
  | `allowedHosts` | `string[]` | Allowed hosts for DNS rebinding protection |
61
61
  | `cors` | `CorsOptions \| boolean` | CORS config. `false` to disable, `true`/omit for permissive defaults (`origin: '*'`), or a [cors](https://www.npmjs.com/package/cors) options object. MCP-required headers are always exposed. |
62
+ | `filterActions` | `FilterActions` | Per-request tool filter - see [Per-request tool filtering](#per-request-tool-filtering-filteractions) |
63
+ | `onToolCall` | `OnToolCall` | Telemetry hook - see [Telemetry](#telemetry-ontoolcall) |
64
+
65
+ #### Per-request tool filtering (`filterActions`)
66
+
67
+ Because the transport is stateless, the tool list is recomputed on **every** request - which makes per-request scoping (per-API-key permissions, tool groups, read-only keys) a single callback:
68
+
69
+ ```typescript
70
+ http({
71
+ host: 'localhost', port: 8080,
72
+ filterActions: async (actions, request) => {
73
+ // request: { headers, url, method, toolName? }
74
+ if (request.method === 'initialize' || request.method === 'ping') { return actions } // skip the DB lookup
75
+ const key = await lookupApiKey(request.headers.authorization)
76
+ if (!key) { throw new SilkweaveError('invalid api key', 'invalid_key', 401) }
77
+ return actions.filter((action) => action.tags?.some((tag) => key.allowedTags.includes(tag)))
78
+ }
79
+ })
80
+ ```
81
+
82
+ - Applies to `tools/list` **and** `tools/call` alike - a client that cached a wider list is still denied.
83
+ - `request.method` is the JSON-RPC method of the POSTed message; `request.toolName` is `params.name` on `tools/call`. Both double as an observability tap (e.g. counting `tools/list`).
84
+ - **Error semantics**: a thrown `SilkweaveError` propagates as its `statusCode` (401/403/...) with a JSON-RPC error body - SDK clients surface it as an auth failure. Any other throw maps to 500. A throw never produces an empty tool list; return `[]` explicitly if "no tools" is the intended answer.
85
+ - Permission changes apply on the next `tools/list` (clients refetch on reconnect) - no `listChanged` session machinery needed or offered.
86
+ - The same option exists on `mcpTransport()`, `@silkweave/edge`'s `edge()`, and `@silkweave/nestjs`'s `mcp()`. Actions carry optional `tags: string[]` as the natural thing to filter on.
87
+
88
+ #### Telemetry (`onToolCall`)
89
+
90
+ One hook observes every tool call - available on `stdio()`, `http()`, `mcpTransport()`, and `@silkweave/edge`'s `edge()`:
91
+
92
+ ```typescript
93
+ http({
94
+ host: 'localhost', port: 8080,
95
+ onToolCall: (event) => {
96
+ // { action, tool, transport: 'mcp', durationMs, ok, errorCode?, errorMessage?, resultBytes?, sideloaded?, context }
97
+ const auth = event.context.getOptional('auth')
98
+ console.log(JSON.stringify({ event: 'tool_call', ...event, context: undefined, userId: auth?.userId }))
99
+ }
100
+ })
101
+ ```
102
+
103
+ - **Fire-and-forget**: never awaited on the result path; sync throws and async rejections are logged and swallowed - the hook can never fail, slow, or reorder a call.
104
+ - Fires after result formatting, so events carry `resultBytes` (serialized raw-result size) and `sideloaded` (whether `smartToolResult` offloaded to an embedded resource).
105
+ - `ok` is `false` when the action threw (with `errorCode`/`errorMessage` - a `SilkweaveError`'s `code`, else the error's name) or when the formatted result is an `isError` tool result.
106
+ - Streaming actions report `durationMs` across full generator consumption.
107
+ - `@silkweave/nestjs` wires this through DI instead - `forRoot({ telemetry: MyTelemetryService })` covers MCP **and** tRPC calls.
62
108
 
63
109
  ### cliProxy
64
110
 
@@ -163,21 +209,62 @@ const MyAction = createAction({
163
209
 
164
210
  Return `undefined` from `toolResult` to fall through to the default `smartToolResult` behavior.
165
211
 
166
- ### Default `disposition`
212
+ ### `disposition`
167
213
 
168
- For the common case of simply choosing `jsonToolResult` over `smartToolResult` (without a hook), set `disposition` on the action:
214
+ Tool results default to compact JSON (`jsonToolResult`). Set `disposition` on the action to change the format:
169
215
 
170
216
  ```typescript
171
217
  createAction({
172
218
  name: 'my-action',
173
- description: 'Returns compact JSON by default',
219
+ description: 'Sideloads large payloads to an embedded resource',
174
220
  input: z.object({}),
175
- disposition: 'json', // 'json' ⇒ jsonToolResult, 'smart' (default) ⇒ smartToolResult
221
+ disposition: 'smart', // 'json' (default) ⇒ jsonToolResult, 'smart' ⇒ smartToolResult
176
222
  run: async () => fetchData()
177
223
  })
178
224
  ```
179
225
 
180
- This is only a **default** - a client that sends `_meta.disposition` on the tool call always wins. Resolution order: client `_meta.disposition` → action `disposition` → `'smart'`. (`@silkweave/nestjs` exposes this as `@Mcp({ result: 'json' })`.)
226
+ `'json'` and `'smart'` are only **defaults** - a client that sends `_meta.disposition` on the tool call always wins. Resolution order: client `_meta.disposition` → action `disposition` → `'json'`. (`@silkweave/nestjs` exposes this as `@Mcp({ result })`.)
227
+
228
+ > **Breaking change in 3.2:** the fallback default flipped from `'smart'` to `'json'`. Set `disposition: 'smart'` per action (or `defaultResult: 'smart'` in `@silkweave/nestjs`) to restore payload sideloading.
229
+
230
+ ### Structured output (`disposition: 'structured'`)
231
+
232
+ A structured action declares its `output` Zod schema as the tool's MCP `outputSchema` - visible to agents in `tools/list` before they call, and returned as `structuredContent`:
233
+
234
+ ```typescript
235
+ createAction({
236
+ name: 'users.get',
237
+ description: 'Get a user by id',
238
+ input: z.object({ id: z.string() }),
239
+ output: z.object({ id: z.string(), name: z.string() }),
240
+ disposition: 'structured',
241
+ run: async ({ id }) => getUser(id) // may return a wider object - extra fields are stripped
242
+ })
243
+ ```
244
+
245
+ Important semantics (the MCP SDK enforces output schemas on **both** sides - the server validates before responding and SDK clients validate independently against `tools/list` - so the schema is a hard contract, not a hint):
246
+
247
+ - The result is **parsed through `output` before shipping**: `structuredContent` is the parsed (extra-fields-stripped) data, plus a JSON text mirror in `content`. Returning a wider object than the schema is therefore safe by construction.
248
+ - A genuine mismatch (missing required field, wrong type) returns an **`isError` tool result** naming the failing fields - not an opaque protocol error - since `isError` results are exempt from SDK output validation.
249
+ - `_meta.disposition` is **ignored** for structured actions; the contract is fixed at `tools/list` time.
250
+ - `'structured'` requires a non-streaming action with an `output` schema - validated at registration (`validateActionDisposition()`), so misconfiguration fails at boot.
251
+ - Fields that can be `null` at runtime must be declared `.nullable()` (zod's `.optional()` does not accept `null`).
252
+
253
+ ### Tool annotations
254
+
255
+ Every tool is registered with MCP `annotations` - behavior hints clients use to group and permission-gate tools. The registrar derives `readOnlyHint` from the action's `kind` (`'query'` ⇒ `true`, otherwise `false`) and merges the action's explicit `annotations` over that base:
256
+
257
+ ```typescript
258
+ createAction({
259
+ name: 'campaigns.delete',
260
+ description: 'Delete a campaign permanently',
261
+ input: z.object({ id: z.string() }),
262
+ annotations: { destructiveHint: true, idempotentHint: true }, // merged over { readOnlyHint: false }
263
+ run: async ({ id }) => remove(id)
264
+ })
265
+ ```
266
+
267
+ All hints are advisory (`title`, `readOnlyHint`, `destructiveHint`, `idempotentHint`, `openWorldHint` - see `ToolAnnotations` in `@silkweave/core`). `@silkweave/nestjs` derives them from the HTTP verb instead (`@Get` ⇒ read-only + idempotent, `@Delete` ⇒ destructive + idempotent) with an `@Mcp({ annotations })` override.
181
268
 
182
269
  ## MCP Result Utilities
183
270
 
@@ -185,8 +272,9 @@ All result utilities are exported from `@silkweave/mcp`:
185
272
 
186
273
  | Function | Description |
187
274
  |----------|-------------|
188
- | `smartToolResult(data)` | Default formatter with automatic embedded resource splitting at 4096 chars |
189
- | `jsonToolResult(data, isError?)` | Simple inline `TextContent` JSON (no splitting) |
275
+ | `smartToolResult(data)` | Formatter with automatic embedded resource splitting at 4096 chars (`disposition: 'smart'`) |
276
+ | `jsonToolResult(data, isError?)` | Simple inline `TextContent` JSON (no splitting) - the default formatter |
277
+ | `structuredToolResult(data)` | `structuredContent` + JSON text mirror for `disposition: 'structured'` actions. Pass output-schema-**parsed** data, never the raw result |
190
278
  | `errorToolResult(error)` | Format a `SilkweaveError` as an error result |
191
279
  | `handleToolError(error)` | Catch-all error handler used by all MCP adapters |
192
280
 
@@ -1,4 +1,4 @@
1
- import { i as parseResourceMessage } from "./result-B_9Eo1ey.mjs";
1
+ import { i as parseResourceMessage } from "./result-CXgeE8ca.mjs";
2
2
  import { createConsoleLogger } from "@silkweave/core";
3
3
  import { kebabCase } from "change-case";
4
4
  import { randomUUID } from "crypto";
package/build/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
- import { a as smartToolResult, c as requestFromExtra, i as parseResourceMessage, n as handleToolError, o as RegisterToolsOptions, r as jsonToolResult, s as registerTools, t as errorToolResult } from "./result-_Bg2cDAG.mjs";
1
+ import { a as smartToolResult, c as registerTools, d as FilterRequest, f as filterErrorResponse, i as parseResourceMessage, l as requestFromExtra, n as handleToolError, o as structuredToolResult, p as rpcInfo, r as jsonToolResult, s as RegisterToolsOptions, t as errorToolResult, u as FilterActions } from "./result-I04HIJR5.mjs";
2
2
  import { CreateMcpExpressAppOptions } from "@modelcontextprotocol/sdk/server/express.js";
3
- import { Action, AdapterFactory, SilkweaveContext, SilkweaveOptions } from "@silkweave/core";
3
+ import { Action, AdapterFactory, OnToolCall, SilkweaveContext, SilkweaveOptions } from "@silkweave/core";
4
4
  import { Express, RequestHandler } from "express";
5
5
  import { AuthConfig, AuthInfo } from "@silkweave/auth";
6
6
  import { AsyncLocalStorage } from "node:async_hooks";
@@ -18,6 +18,17 @@ interface StartMcpHttpOptions extends CreateMcpExpressAppOptions {
18
18
  sideloadResources?: boolean;
19
19
  /** Directory the sideload route reads from. Default `'resources'`. */
20
20
  resourceDir?: string;
21
+ /**
22
+ * Per-request tool filter, applied before `registerTools()` on every
23
+ * `POST /mcp` (the stateless transport recomputes the tool list per request,
24
+ * so permission changes apply on the next `tools/list`). See `FilterActions`
25
+ * for the request stand-in (`headers`/`url`/`method`/`toolName`) and error
26
+ * semantics (a throw surfaces as its `SilkweaveError.statusCode` or 500 -
27
+ * never an empty tool list).
28
+ */
29
+ filterActions?: FilterActions;
30
+ /** Telemetry hook invoked once per tool call (fire-and-forget). */
31
+ onToolCall?: OnToolCall;
21
32
  }
22
33
  /**
23
34
  * Build a fully-wired Express app that exposes the MCP Streamable HTTP
@@ -41,7 +52,11 @@ declare function startMcpServer(silkweaveOptions: SilkweaveOptions, actions: Act
41
52
  declare const http: AdapterFactory<StartMcpHttpOptions>;
42
53
  //#endregion
43
54
  //#region src/adapter/stdio.d.ts
44
- declare const stdio: AdapterFactory;
55
+ interface StdioAdapterOptions {
56
+ /** Telemetry hook invoked once per tool call (fire-and-forget). */
57
+ onToolCall?: OnToolCall;
58
+ }
59
+ declare const stdio: AdapterFactory<StdioAdapterOptions | void>;
45
60
  //#endregion
46
61
  //#region src/handlers/auth.d.ts
47
62
  /**
@@ -127,6 +142,15 @@ interface McpTransportHandlers {
127
142
  /** `POST /mcp` - handle a single stateless MCP request/response (SSE per call). */
128
143
  post: RequestHandler;
129
144
  }
145
+ interface McpTransportOptions {
146
+ /**
147
+ * Per-request tool filter, applied before `registerTools()` on every POST.
148
+ * See `FilterActions` for the request stand-in and error semantics.
149
+ */
150
+ filterActions?: FilterActions;
151
+ /** Telemetry hook invoked once per tool call (fire-and-forget). */
152
+ onToolCall?: OnToolCall;
153
+ }
130
154
  /**
131
155
  * Build the MCP Streamable HTTP transport route handler.
132
156
  *
@@ -136,7 +160,7 @@ interface McpTransportHandlers {
136
160
  * and tears down on response close. No `Mcp-Session-Id`, no session map, no
137
161
  * `GET`/`DELETE` reconnect - any request can hit any instance.
138
162
  */
139
- declare function mcpTransport(silkweaveOptions: SilkweaveOptions, context: SilkweaveContext, actions: Action[]): McpTransportHandlers;
163
+ declare function mcpTransport(silkweaveOptions: SilkweaveOptions, context: SilkweaveContext, actions: Action[], options?: McpTransportOptions): McpTransportHandlers;
140
164
  //#endregion
141
165
  //#region src/util/sideload.d.ts
142
166
  interface SideloadResource {
@@ -150,5 +174,5 @@ declare function createSideloadResource(buffer: Buffer, {
150
174
  contentType
151
175
  }: Pick<SideloadResource, 'name' | 'contentType'>): Promise<SideloadResource>;
152
176
  //#endregion
153
- export { type CorsOptions, MCP_REQUIRED_HEADERS, McpTransportHandlers, OAuthRouteHandlers, RegisterToolsOptions, SideloadResource, SideloadResourceOptions, StartMcpHttpOptions, authMiddleware, authStorage, buildMcpExpressApp, createSideloadResource, errorToolResult, handleToolError, http, jsonToolResult, mcpCors, mcpTransport, oauthRoutes, parseResourceMessage, protectedResourceMetadata, registerTools, requestFromExtra, sideloadResource, smartToolResult, startMcpServer, stdio };
177
+ export { type CorsOptions, FilterActions, FilterRequest, MCP_REQUIRED_HEADERS, McpTransportHandlers, McpTransportOptions, OAuthRouteHandlers, RegisterToolsOptions, SideloadResource, SideloadResourceOptions, StartMcpHttpOptions, StdioAdapterOptions, authMiddleware, authStorage, buildMcpExpressApp, createSideloadResource, errorToolResult, filterErrorResponse, handleToolError, http, jsonToolResult, mcpCors, mcpTransport, oauthRoutes, parseResourceMessage, protectedResourceMetadata, registerTools, requestFromExtra, rpcInfo, sideloadResource, smartToolResult, startMcpServer, stdio, structuredToolResult };
154
178
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/adapter/http.ts","../src/adapter/stdio.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","../src/util/sideload.ts"],"mappings":";;;;;;;;;;UAaiB,mBAAA,SAA4B,0BAAA;EAC3C,IAAA;EACA,IAAA;EACA,IAAA,GAAO,UAAA;EAHQ;EAKf,IAAA,GAAO,aAAA;;EAEP,iBAAA;EAFO;EAIP,WAAA;AAAA;;;;;;;;iBAUc,kBAAA,CACd,gBAAA,EAAkB,gBAAA,EAClB,OAAA,EAAS,gBAAA,EACT,OAAA,EAAS,MAAA,IACT,OAAA,EAAS,mBAAA,GACR,OAAA;;;;;AALH;;iBAmDsB,cAAA,CACpB,gBAAA,EAAkB,gBAAA,EAClB,OAAA,EAAS,MAAA,IACT,OAAA,EAAS,mBAAA,EACT,OAAA,GAAU,gBAAA,GACT,OAAA,CAAQ,MAAA;;;;;cAgBE,IAAA,EAAM,cAAA,CAAe,mBAAA;;;cCnGrB,KAAA,EAAO,cAAA;;;;;;;cCIP,WAAA,EAAW,iBAAA,CAAA,QAAA;;AFIxB;;;;;;;;;iBEQgB,cAAA,CAAe,IAAA,EAAM,UAAA,EAAY,OAAA,EAAS,gBAAA,GAAmB,cAAA;;;;cCjBhE,oBAAA;;;;;;;AHSb;;iBGCgB,OAAA,CAAQ,UAAA,GAAY,aAAA,aAA+B,cAAA;;;;;;;;iBCNnD,yBAAA,CAA0B,IAAA,EAAM,UAAA,GAAa,cAAA;;;UCa5C,kBAAA;;EAEf,mBAAA,EAAqB,cAAA;;EAErB,SAAA,EAAW,cAAA;;EAEX,QAAA,EAAU,cAAA;;EAEV,YAAA;ELhBmC;EKkBnC,KAAA,EAAO,cAAA;ELfA;EKiBP,QAAA,EAAU,cAAA;AAAA;;;;;;;;;;;iBAaI,WAAA,CAAY,IAAA,EAAM,UAAA,GAAa,kBAAA;;;UC1C9B,uBAAA;;EAEf,WAAA;AAAA;;;;;;ANOF;iBMEgB,gBAAA,CAAiB,OAAA,GAAS,uBAAA,GAA+B,cAAA;;;UCGxD,oBAAA;;EAEf,IAAA,EAAM,cAAA;AAAA;;;;;APPR;;;;;iBOmBgB,YAAA,CACd,gBAAA,EAAkB,gBAAA,EAClB,OAAA,EAAS,gBAAA,EACT,OAAA,EAAS,MAAA,KACR,oBAAA;;;UCjCc,gBAAA;EACf,EAAA;EACA,IAAA;EACA,WAAA;EACA,IAAA;AAAA;AAAA,iBAGoB,sBAAA,CAAuB,MAAA,EAAQ,MAAA;EAAU,IAAA;EAAM;AAAA,GAAe,IAAA,CAAK,gBAAA,4BAAyC,OAAA,CAAA,gBAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/adapter/http.ts","../src/adapter/stdio.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","../src/util/sideload.ts"],"mapp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;;ANOF;iBMEgB,gBAAA,CAAiB,OAAA,GAAS,uBAAA,GAA+B,cAAA;;;UCGxD,oBAAA;;EAEf,IAAA,EAAM,cAAA;AAAA;AAAA,UAGS,mBAAA;;;APVjB;;EOeE,aAAA,GAAgB,aAAA;EPZT;EOcP,UAAA,GAAa,UAAA;AAAA;;;;;;;;;;iBAYC,YAAA,CACd,gBAAA,EAAkB,gBAAA,EAClB,OAAA,EAAS,gBAAA,EACT,OAAA,EAAS,MAAA,IACT,OAAA,GAAS,mBAAA,GACR,oBAAA;;;UC7Cc,gBAAA;EACf,EAAA;EACA,IAAA;EACA,WAAA;EACA,IAAA;AAAA;AAAA,iBAGoB,sBAAA,CAAuB,MAAA,EAAQ,MAAA;EAAU,IAAA;EAAM;AAAA,GAAe,IAAA,CAAK,gBAAA,4BAAyC,OAAA,CAAA,gBAAA"}
package/build/index.mjs CHANGED
@@ -1,11 +1,12 @@
1
- import { i as authStorage, n as requestFromExtra, r as authMiddleware, t as registerTools } from "./registerTools-Z6JnoM60.mjs";
2
- import { a as smartToolResult, i as parseResourceMessage, n as handleToolError, r as jsonToolResult, t as errorToolResult } from "./result-B_9Eo1ey.mjs";
1
+ import { a as authMiddleware, i as rpcInfo, n as requestFromExtra, o as authStorage, r as filterErrorResponse, t as registerTools } from "./registerTools-B_vjxOrs.mjs";
2
+ import { a as smartToolResult, i as parseResourceMessage, n as handleToolError, o as structuredToolResult, r as jsonToolResult, t as errorToolResult } from "./result-CXgeE8ca.mjs";
3
3
  import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
4
- import { createContext } from "@silkweave/core";
4
+ import { createContext, validateActionDisposition } from "@silkweave/core";
5
5
  import express from "express";
6
6
  import { generateProtectedResourceMetadata } from "@silkweave/auth";
7
7
  import cors from "cors";
8
8
  import { readFile, writeFile } from "fs/promises";
9
+ import { basename, resolve, sep } from "path";
9
10
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
10
11
  import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
11
12
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
@@ -107,11 +108,18 @@ function oauthRoutes(auth) {
107
108
  */
108
109
  function sideloadResource(options = {}) {
109
110
  const { resourceDir = "resources" } = options;
111
+ const baseDir = resolve(resourceDir);
110
112
  return async (req, res) => {
111
113
  const id = req.params["id"];
112
114
  if (!id || typeof id !== "string") throw new Error("Invalid ID");
113
- const resourceMeta = JSON.parse(await readFile(`${resourceDir}/${id}.json`, "utf-8"));
114
- const buffer = await readFile(`${resourceDir}/${id}`);
115
+ const safeId = basename(id);
116
+ const target = resolve(baseDir, safeId);
117
+ if (safeId !== id || target !== baseDir && !target.startsWith(baseDir + sep)) {
118
+ res.status(400).json({ error: "invalid_resource_id" });
119
+ return;
120
+ }
121
+ const resourceMeta = JSON.parse(await readFile(`${target}.json`, "utf-8"));
122
+ const buffer = await readFile(target);
115
123
  res.status(200);
116
124
  res.header("Content-Type", resourceMeta.contentType);
117
125
  res.send(buffer);
@@ -119,7 +127,7 @@ function sideloadResource(options = {}) {
119
127
  }
120
128
  //#endregion
121
129
  //#region src/handlers/transport.ts
122
- function createMcpServer(options, actions, context) {
130
+ function createMcpServer(options, actions, context, onToolCall) {
123
131
  const server = new McpServer({
124
132
  name: options.name,
125
133
  description: options.description,
@@ -128,7 +136,7 @@ function createMcpServer(options, actions, context) {
128
136
  tools: {},
129
137
  logging: {}
130
138
  } });
131
- registerTools(server, actions, context);
139
+ registerTools(server, actions, context, { onToolCall });
132
140
  return server;
133
141
  }
134
142
  /**
@@ -140,11 +148,35 @@ function createMcpServer(options, actions, context) {
140
148
  * and tears down on response close. No `Mcp-Session-Id`, no session map, no
141
149
  * `GET`/`DELETE` reconnect - any request can hit any instance.
142
150
  */
143
- function mcpTransport(silkweaveOptions, context, actions) {
151
+ function mcpTransport(silkweaveOptions, context, actions, options = {}) {
152
+ actions.forEach(validateActionDisposition);
144
153
  const post = async (req, res) => {
154
+ if (Array.isArray(req.body)) {
155
+ res.status(400).json({
156
+ jsonrpc: "2.0",
157
+ error: {
158
+ code: -32600,
159
+ message: "JSON-RPC batch requests are not supported"
160
+ },
161
+ id: null
162
+ });
163
+ return;
164
+ }
165
+ let active = actions;
166
+ if (options.filterActions) try {
167
+ active = await options.filterActions(actions, {
168
+ headers: req.headers,
169
+ url: req.originalUrl ?? req.url,
170
+ ...rpcInfo(req.body)
171
+ });
172
+ } catch (error) {
173
+ const { status, body } = filterErrorResponse(error, req.body);
174
+ res.status(status).json(body);
175
+ return;
176
+ }
145
177
  try {
146
178
  const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
147
- const server = createMcpServer(silkweaveOptions, actions, context);
179
+ const server = createMcpServer(silkweaveOptions, active, context, options.onToolCall);
148
180
  res.on("close", () => {
149
181
  transport.close();
150
182
  server.close();
@@ -175,7 +207,7 @@ function mcpTransport(silkweaveOptions, context, actions) {
175
207
  * top-level `startMcpServer()` / `http()` adapter conveniences.
176
208
  */
177
209
  function buildMcpExpressApp(silkweaveOptions, context, actions, options) {
178
- const { host, auth, cors: corsConfig, sideloadResources = true, resourceDir, ...mcpAppOptions } = options;
210
+ const { host, auth, cors: corsConfig, sideloadResources = true, resourceDir, filterActions, onToolCall, ...mcpAppOptions } = options;
179
211
  const app = createMcpExpressApp({
180
212
  ...mcpAppOptions,
181
213
  host
@@ -207,7 +239,10 @@ function buildMcpExpressApp(silkweaveOptions, context, actions, options) {
207
239
  });
208
240
  }
209
241
  if (sideloadResources) app.get("/resource/:id", sideloadResource({ resourceDir }));
210
- const transport = mcpTransport(silkweaveOptions, context, actions);
242
+ const transport = mcpTransport(silkweaveOptions, context, actions, {
243
+ filterActions,
244
+ onToolCall
245
+ });
211
246
  app.post("/mcp", express.json(), transport.post);
212
247
  return app;
213
248
  }
@@ -265,7 +300,7 @@ const http = (options) => {
265
300
  };
266
301
  //#endregion
267
302
  //#region src/adapter/stdio.ts
268
- const stdio = () => {
303
+ const stdio = (adapterOptions) => {
269
304
  return (options, baseContext) => {
270
305
  const context = baseContext.fork({ adapter: "stdio" });
271
306
  const server = new McpServer({
@@ -279,7 +314,11 @@ const stdio = () => {
279
314
  return {
280
315
  context,
281
316
  start: async (actions) => {
282
- registerTools(server, actions, context, { logStream: false });
317
+ actions.forEach(validateActionDisposition);
318
+ registerTools(server, actions, context, {
319
+ logStream: false,
320
+ onToolCall: adapterOptions?.onToolCall
321
+ });
283
322
  const transport = new StdioServerTransport();
284
323
  await server.connect(transport);
285
324
  },
@@ -303,6 +342,6 @@ async function createSideloadResource(buffer, { name, contentType }) {
303
342
  return resource;
304
343
  }
305
344
  //#endregion
306
- export { MCP_REQUIRED_HEADERS, authMiddleware, authStorage, buildMcpExpressApp, createSideloadResource, errorToolResult, handleToolError, http, jsonToolResult, mcpCors, mcpTransport, oauthRoutes, parseResourceMessage, protectedResourceMetadata, registerTools, requestFromExtra, sideloadResource, smartToolResult, startMcpServer, stdio };
345
+ export { MCP_REQUIRED_HEADERS, authMiddleware, authStorage, buildMcpExpressApp, createSideloadResource, errorToolResult, filterErrorResponse, handleToolError, http, jsonToolResult, mcpCors, mcpTransport, oauthRoutes, parseResourceMessage, protectedResourceMetadata, registerTools, requestFromExtra, rpcInfo, sideloadResource, smartToolResult, startMcpServer, stdio, structuredToolResult };
307
346
 
308
347
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/handlers/cors.ts","../src/handlers/metadata.ts","../src/handlers/oauth.ts","../src/handlers/sideload.ts","../src/handlers/transport.ts","../src/adapter/http.ts","../src/adapter/stdio.ts","../src/util/sideload.ts"],"sourcesContent":["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 { 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 return async (req, res) => {\n const id = req.params['id']\n if (!id || typeof id !== 'string') { throw new Error('Invalid ID') }\n const resourceMeta: SideloadResource = JSON.parse(await readFile(`${resourceDir}/${id}.json`, 'utf-8'))\n const buffer = await readFile(`${resourceDir}/${id}`)\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, SilkweaveContext, SilkweaveOptions } from '@silkweave/core'\nimport { type RequestHandler } from 'express'\nimport { registerTools } from './registerTools.js'\n\nfunction createMcpServer(options: SilkweaveOptions, actions: Action[], context: SilkweaveContext): 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)\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\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): McpTransportHandlers {\n const post: RequestHandler = async (req, res) => {\n try {\n const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined })\n const server = createMcpServer(silkweaveOptions, actions, context)\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, 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 { 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\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, ...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)\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","import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'\nimport { AdapterFactory } from '@silkweave/core'\nimport { registerTools } from '../handlers/registerTools.js'\n\nexport const stdio: AdapterFactory = () => {\n return (options, baseContext) => {\n const context = baseContext.fork({ adapter: 'stdio' })\n const server = new McpServer({\n name: options.name,\n description: options.description,\n version: options.version\n }, {\n capabilities: { tools: {}, logging: {} }\n })\n return {\n context,\n start: async (actions) => {\n registerTools(server, actions, context, { logStream: false })\n const transport = new StdioServerTransport()\n await server.connect(transport)\n },\n stop: async () => {\n await server?.close()\n }\n }\n }\n}\n","import { randomUUID } from 'crypto'\nimport { writeFile } from 'fs/promises'\n\nexport interface SideloadResource {\n id: string\n name: string\n contentType: string\n size: number\n}\n\nexport async function createSideloadResource(buffer: Buffer, { name, contentType }: Pick<SideloadResource, 'name' | 'contentType'>) {\n const id = randomUUID()\n const resource: SideloadResource = { id, name, contentType, size: buffer.length }\n await Promise.all([\n writeFile(`resources/${id}`, buffer),\n writeFile(`resources/${id}.json`, JSON.stringify(resource), 'utf-8')\n ])\n return resource\n}\n"],"mappings":";;;;;;;;;;;;;;AAIA,MAAa,uBAAuB;CAAC;CAAoB;CAAiB;CAAuB;;;;;;;;;AAUjG,SAAgB,QAAQ,aAAoC,MAA6B;AACvF,KAAI,eAAe,MAAS,QAAO;CACnC,MAAM,aAAa,eAAe,OAAO,EAAE,GAAG;CAC9C,MAAM,cAAc,WAAW;CAC/B,MAAM,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AM;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;;;;;AC1HL,MAAa,cAA8B;AACzC,SAAQ,SAAS,gBAAgB;EAC/B,MAAM,UAAU,YAAY,KAAK,EAAE,SAAS,SAAS,CAAC;EACtD,MAAM,SAAS,IAAI,UAAU;GAC3B,MAAM,QAAQ;GACd,aAAa,QAAQ;GACrB,SAAS,QAAQ;GAClB,EAAE,EACD,cAAc;GAAE,OAAO,EAAE;GAAE,SAAS,EAAE;GAAE,EACzC,CAAC;AACF,SAAO;GACL;GACA,OAAO,OAAO,YAAY;AACxB,kBAAc,QAAQ,SAAS,SAAS,EAAE,WAAW,OAAO,CAAC;IAC7D,MAAM,YAAY,IAAI,sBAAsB;AAC5C,UAAM,OAAO,QAAQ,UAAU;;GAEjC,MAAM,YAAY;AAChB,UAAM,QAAQ,OAAO;;GAExB;;;;;ACfL,eAAsB,uBAAuB,QAAgB,EAAE,MAAM,eAA+D;CAClI,MAAM,KAAK,YAAY;CACvB,MAAM,WAA6B;EAAE;EAAI;EAAM;EAAa,MAAM,OAAO;EAAQ;AACjF,OAAM,QAAQ,IAAI,CAChB,UAAU,aAAa,MAAM,OAAO,EACpC,UAAU,aAAa,GAAG,QAAQ,KAAK,UAAU,SAAS,EAAE,QAAQ,CACrE,CAAC;AACF,QAAO"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/handlers/cors.ts","../src/handlers/metadata.ts","../src/handlers/oauth.ts","../src/handlers/sideload.ts","../src/handlers/transport.ts","../src/adapter/http.ts","../src/adapter/stdio.ts","../src/util/sideload.ts"],"sourcesContent":["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 { 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 const server = createMcpServer(silkweaveOptions, active, context, 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","import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'\nimport { AdapterFactory, OnToolCall, validateActionDisposition } from '@silkweave/core'\nimport { registerTools } from '../handlers/registerTools.js'\n\nexport interface StdioAdapterOptions {\n /** Telemetry hook invoked once per tool call (fire-and-forget). */\n onToolCall?: OnToolCall\n}\n\nexport const stdio: AdapterFactory<StdioAdapterOptions | void> = (adapterOptions) => {\n return (options, baseContext) => {\n const context = baseContext.fork({ adapter: 'stdio' })\n const server = new McpServer({\n name: options.name,\n description: options.description,\n version: options.version\n }, {\n capabilities: { tools: {}, logging: {} }\n })\n return {\n context,\n start: async (actions) => {\n actions.forEach(validateActionDisposition)\n registerTools(server, actions, context, { logStream: false, onToolCall: adapterOptions?.onToolCall })\n const transport = new StdioServerTransport()\n await server.connect(transport)\n },\n stop: async () => {\n await server?.close()\n }\n }\n }\n}\n","import { randomUUID } from 'crypto'\nimport { writeFile } from 'fs/promises'\n\nexport interface SideloadResource {\n id: string\n name: string\n contentType: string\n size: number\n}\n\nexport async function createSideloadResource(buffer: Buffer, { name, contentType }: Pick<SideloadResource, 'name' | 'contentType'>) {\n const id = randomUUID()\n const resource: SideloadResource = { id, name, contentType, size: buffer.length }\n await Promise.all([\n writeFile(`resources/${id}`, buffer),\n writeFile(`resources/${id}.json`, JSON.stringify(resource), 'utf-8')\n ])\n return resource\n}\n"],"mappings":";;;;;;;;;;;;;;;AAIA,MAAa,uBAAuB;CAAC;CAAoB;CAAiB;CAAuB;;;;;;;;;AAUjG,SAAgB,QAAQ,aAAoC,MAA6B;AACvF,KAAI,eAAe,MAAS,QAAO;CACnC,MAAM,aAAa,eAAe,OAAO,EAAE,GAAG;CAC9C,MAAM,cAAc,WAAW;CAC/B,MAAM,iBAAiB,CACrB,GAAG,sBACH,GAAI,MAAM,QAAQ,YAAY,GAAG,cAAc,cAAc,CAAC,YAAY,GAAG,EAAE,CAChF;AACD,QAAO,KAAK;EAAE,QAAQ;EAAK,GAAG;EAAY;EAAgB,CAAC;;;;;;;;;ACd7D,SAAgB,0BAA0B,MAAkC;AAC1E,KAAI,CAAC,KAAK,eAAe,CAAC,KAAK,sBAAsB,OACnD,OAAM,IAAI,MAAM,0GAA0G;CAE5H,MAAM,WAAW,kCAAkC,KAAK,aAAa,KAAK,sBAAsB,KAAK,eAAe;AACpH,SAAQ,MAAM,QAAQ;AAAE,MAAI,KAAK,SAAS;;;;;ACV5C,SAAS,WAAW,KAA4B;AAC9C,QAAO;EACL,QAAQ,IAAI;EACZ,KAAK,IAAI,IAAI,IAAI,KAAK,GAAG,IAAI,SAAS,KAAK,IAAI,IAAI,OAAO,GAAG;EAC7D,SAAS,OAAO,YAAY,OAAO,QAAQ,IAAI,QAAQ,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,GAAG,MAAM,QAAQ,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;EAC1G,MAAM,IAAI;EACX;;AAGH,SAAS,UAAU,KAAe,UAAyB;AACzD,MA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,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;;;;;ACjIL,MAAa,SAAqD,mBAAmB;AACnF,SAAQ,SAAS,gBAAgB;EAC/B,MAAM,UAAU,YAAY,KAAK,EAAE,SAAS,SAAS,CAAC;EACtD,MAAM,SAAS,IAAI,UAAU;GAC3B,MAAM,QAAQ;GACd,aAAa,QAAQ;GACrB,SAAS,QAAQ;GAClB,EAAE,EACD,cAAc;GAAE,OAAO,EAAE;GAAE,SAAS,EAAE;GAAE,EACzC,CAAC;AACF,SAAO;GACL;GACA,OAAO,OAAO,YAAY;AACxB,YAAQ,QAAQ,0BAA0B;AAC1C,kBAAc,QAAQ,SAAS,SAAS;KAAE,WAAW;KAAO,YAAY,gBAAgB;KAAY,CAAC;IACrG,MAAM,YAAY,IAAI,sBAAsB;AAC5C,UAAM,OAAO,QAAQ,UAAU;;GAEjC,MAAM,YAAY;AAChB,UAAM,QAAQ,OAAO;;GAExB;;;;;ACrBL,eAAsB,uBAAuB,QAAgB,EAAE,MAAM,eAA+D;CAClI,MAAM,KAAK,YAAY;CACvB,MAAM,WAA6B;EAAE;EAAI;EAAM;EAAa,MAAM,OAAO;EAAQ;AACjF,OAAM,QAAQ,IAAI,CAChB,UAAU,aAAa,MAAM,OAAO,EACpC,UAAU,aAAa,GAAG,QAAQ,KAAK,UAAU,SAAS,EAAE,QAAQ,CACrE,CAAC;AACF,QAAO"}
@@ -0,0 +1,257 @@
1
+ import { a as smartToolResult, n as handleToolError, o as structuredToolResult, r as jsonToolResult, t as errorToolResult } from "./result-CXgeE8ca.mjs";
2
+ import { SilkweaveError, createLogger, emitToolCall, isStreamingAction, runStreamingAction } from "@silkweave/core";
3
+ import { validateToken } from "@silkweave/auth";
4
+ import { AsyncLocalStorage } from "node:async_hooks";
5
+ import { capitalCase, pascalCase } from "change-case";
6
+ //#region src/handlers/auth.ts
7
+ /**
8
+ * Per-request bearer-token storage used by tool handlers to read the resolved
9
+ * `AuthInfo` for the currently-handled MCP call.
10
+ */
11
+ const authStorage = new AsyncLocalStorage();
12
+ /**
13
+ * Express middleware that validates the `Authorization: Bearer …` header via
14
+ * the supplied `AuthConfig`. On success the resolved `AuthInfo` is placed in
15
+ * `authStorage` for the duration of the downstream handler - `mcpTransport`'s
16
+ * tool callbacks pick it up to populate the silkweave context's `auth` key.
17
+ *
18
+ * The middleware should NOT be applied to OAuth-discovery / token routes -
19
+ * compose it only on the routes that require an authenticated caller (the MCP
20
+ * transport itself, sideload, etc.).
21
+ */
22
+ function authMiddleware(auth, context) {
23
+ return async (req, res, next) => {
24
+ const result = await validateToken(req.headers.authorization, auth, context.fork({ request: req }));
25
+ if (result.error) {
26
+ for (const [key, value] of Object.entries(result.error.headers)) res.header(key, value);
27
+ res.status(result.error.statusCode).json(result.error.body);
28
+ return;
29
+ }
30
+ if (result.auth) authStorage.run(result.auth, () => {
31
+ next();
32
+ });
33
+ else next();
34
+ };
35
+ }
36
+ //#endregion
37
+ //#region src/handlers/filter.ts
38
+ /**
39
+ * Extract the JSON-RPC `method` (and `toolName` for `tools/call`) from a
40
+ * parsed request body. Tolerates a legacy batch (first request wins) and
41
+ * unrecognizable bodies (empty `method`).
42
+ */
43
+ function rpcInfo(body) {
44
+ const message = Array.isArray(body) ? body[0] : body;
45
+ const method = typeof message?.method === "string" ? message.method : "";
46
+ const toolName = method === "tools/call" && typeof message?.params?.name === "string" ? message.params.name : void 0;
47
+ return {
48
+ method,
49
+ ...toolName !== void 0 ? { toolName } : {}
50
+ };
51
+ }
52
+ /**
53
+ * Map a `filterActions` throw to an HTTP status + JSON-RPC error body: a
54
+ * `SilkweaveError` keeps its `statusCode` (so an invalid key surfaces as an
55
+ * auth failure, not "server has no tools"), anything else is a 500. `id` is
56
+ * echoed from the request body when available.
57
+ */
58
+ function filterErrorResponse(error, body) {
59
+ const id = (Array.isArray(body) ? body[0] : body)?.id ?? null;
60
+ if (error instanceof SilkweaveError) return {
61
+ status: error.statusCode,
62
+ body: {
63
+ jsonrpc: "2.0",
64
+ error: {
65
+ code: -32e3,
66
+ message: error.message
67
+ },
68
+ id
69
+ }
70
+ };
71
+ console.error("filterActions error:", error);
72
+ return {
73
+ status: 500,
74
+ body: {
75
+ jsonrpc: "2.0",
76
+ error: {
77
+ code: -32603,
78
+ message: "Internal server error"
79
+ },
80
+ id
81
+ }
82
+ };
83
+ }
84
+ //#endregion
85
+ //#region src/handlers/registerTools.ts
86
+ /**
87
+ * Build the generic `request` context value (the same key REST/tRPC populate)
88
+ * from the MCP SDK's `extra.requestInfo`, so transport-agnostic consumers - e.g.
89
+ * `@silkweave/nestjs` `@UseGuards` guards reading
90
+ * `switchToHttp().getRequest().headers` - work over MCP. There are no path
91
+ * `params`/`query`/`body` on the raw MCP call, so those start as empty
92
+ * stand-ins; `@silkweave/nestjs` later fills them from the validated tool input
93
+ * per the reflected param sources (path -> `params`, `@Query` -> `query`,
94
+ * `@Body` -> `body`) so guards reading any of them decide as they would over
95
+ * REST. Returns `undefined` when no HTTP request info is available.
96
+ */
97
+ function requestFromExtra(requestInfo) {
98
+ if (!requestInfo) return;
99
+ return {
100
+ headers: requestInfo.headers ?? {},
101
+ url: requestInfo.url?.toString(),
102
+ params: {},
103
+ query: {},
104
+ body: {}
105
+ };
106
+ }
107
+ /** Per-call logger that bridges silkweave logs/progress onto MCP notifications. */
108
+ function createToolLogger(extra, stream) {
109
+ return createLogger({
110
+ stream,
111
+ onLog: (level, data) => {
112
+ extra.sendNotification({
113
+ method: "notifications/message",
114
+ params: {
115
+ level,
116
+ data
117
+ }
118
+ });
119
+ },
120
+ onProgress: ({ progress, total, message }) => {
121
+ if (!extra._meta?.progressToken) return;
122
+ extra.sendNotification({
123
+ method: "notifications/progress",
124
+ params: {
125
+ progress,
126
+ total,
127
+ message,
128
+ progressToken: extra._meta.progressToken
129
+ }
130
+ });
131
+ }
132
+ });
133
+ }
134
+ /** Run the action, streaming chunks as progress notifications when a token is set. */
135
+ async function runAction(action, input, context, extra) {
136
+ const progressToken = extra._meta?.progressToken;
137
+ if (isStreamingAction(action)) return runStreamingAction(action, input, context, progressToken ? async (chunk, index) => {
138
+ await extra.sendNotification({
139
+ method: "notifications/progress",
140
+ params: {
141
+ progressToken,
142
+ progress: index + 1,
143
+ message: JSON.stringify(chunk)
144
+ }
145
+ });
146
+ } : void 0);
147
+ return action.run(input, context);
148
+ }
149
+ /**
150
+ * Result path for a `disposition: 'structured'` action: parse the raw result
151
+ * through the output schema and ship the PARSED data as `structuredContent`.
152
+ * Parsing strips extra fields (so the client-side JSON-Schema validator, which
153
+ * rejects `additionalProperties`, passes by construction) and a genuine
154
+ * mismatch (missing required field, wrong type) degrades to an `isError` tool
155
+ * result - which the SDK exempts from output validation - instead of an opaque
156
+ * protocol error.
157
+ */
158
+ function structuredResult(action, result) {
159
+ const parsed = action.output.safeParse(result);
160
+ if (!parsed.success) {
161
+ const fields = [...new Set(parsed.error.issues.map((issue) => issue.path.join(".") || "(root)"))].join(", ");
162
+ return errorToolResult(new SilkweaveError(`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.`, "output_validation_error"));
163
+ }
164
+ if (!action.output.safeParse(parsed.data).success) return errorToolResult(new SilkweaveError(`Output schema for '${action.name}' is not idempotent (a field-level .transform()?) and cannot back a 'structured' contract - use disposition 'json' or remove the transform.`, "output_schema_not_structurable"));
165
+ return structuredToolResult(parsed.data);
166
+ }
167
+ /** MCP-only telemetry fields, computed only when a hook is registered. */
168
+ function resultMeta(result, formatted) {
169
+ return {
170
+ resultBytes: new TextEncoder().encode(JSON.stringify(result)).length,
171
+ sideloaded: (formatted.content ?? []).some((block) => block.type === "resource")
172
+ };
173
+ }
174
+ /** Error identity for telemetry: a SilkweaveError's `code`, else the error's name. */
175
+ function errorMeta(error) {
176
+ if (error instanceof SilkweaveError) return {
177
+ errorCode: error.code,
178
+ errorMessage: error.message
179
+ };
180
+ if (error instanceof Error) return {
181
+ errorCode: error.name,
182
+ errorMessage: error.message
183
+ };
184
+ return { errorCode: "unknown" };
185
+ }
186
+ /** Format via the action's `toolResult` hook, else the resolved disposition. */
187
+ function formatToolResult(action, result, context, disposition) {
188
+ if (action.toolResult) {
189
+ const response = action.toolResult(result, context);
190
+ if (response) return response;
191
+ }
192
+ if (action.disposition === "structured") return structuredResult(action, result);
193
+ return disposition === "smart" ? smartToolResult(result) : jsonToolResult(result);
194
+ }
195
+ /**
196
+ * Register every action as an MCP tool on `server`. Shared by all MCP transports
197
+ * (`stdio`, `http`, `edge`). Each tool call forks `context` with a per-call
198
+ * `logger`, the SDK `extra`, a synthesized `request` (see `requestFromExtra`),
199
+ * and - when bearer auth ran for this request - the resolved `auth`. The result
200
+ * is formatted by the action's `toolResult` hook if present, otherwise per the
201
+ * resolved disposition (client `_meta.disposition` > `action.disposition` >
202
+ * `json`); a `'structured'` action always ships schema-parsed
203
+ * `structuredContent` (its contract cannot be demoted per-call).
204
+ */
205
+ function registerTools(server, actions, context, options = {}) {
206
+ const stream = options.logStream ?? process.stderr;
207
+ for (const action of actions) server.registerTool(pascalCase(action.name), {
208
+ title: capitalCase(action.name),
209
+ description: action.description,
210
+ inputSchema: action.input,
211
+ annotations: {
212
+ readOnlyHint: action.kind === "query",
213
+ ...action.annotations
214
+ },
215
+ ...action.disposition === "structured" && action.output ? { outputSchema: action.output } : {}
216
+ }, async (input, extra) => {
217
+ const logger = createToolLogger(extra, stream);
218
+ const currentAuth = authStorage.getStore();
219
+ const actionContext = context.fork({
220
+ logger,
221
+ extra,
222
+ request: requestFromExtra(extra.requestInfo),
223
+ ...currentAuth ? { auth: currentAuth } : {}
224
+ });
225
+ const disposition = extra._meta?.disposition ?? action.disposition;
226
+ const base = {
227
+ action: action.name,
228
+ tool: pascalCase(action.name),
229
+ transport: "mcp",
230
+ context: actionContext
231
+ };
232
+ const started = Date.now();
233
+ try {
234
+ const result = await runAction(action, input, actionContext, extra);
235
+ const formatted = formatToolResult(action, result, actionContext, disposition);
236
+ emitToolCall(options.onToolCall, {
237
+ ...base,
238
+ durationMs: Date.now() - started,
239
+ ok: formatted.isError !== true,
240
+ ...options.onToolCall ? resultMeta(result, formatted) : {}
241
+ });
242
+ return formatted;
243
+ } catch (error) {
244
+ emitToolCall(options.onToolCall, {
245
+ ...base,
246
+ durationMs: Date.now() - started,
247
+ ok: false,
248
+ ...errorMeta(error)
249
+ });
250
+ return handleToolError(error);
251
+ }
252
+ });
253
+ }
254
+ //#endregion
255
+ export { authMiddleware as a, rpcInfo as i, requestFromExtra as n, authStorage as o, filterErrorResponse as r, registerTools as t };
256
+
257
+ //# sourceMappingURL=registerTools-B_vjxOrs.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registerTools-B_vjxOrs.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`). Empty string when the body\n * is not a recognizable JSON-RPC message. JSON-RPC batches are rejected by the\n * transport before the filter runs, so this always reflects a single message.\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 // The SDK independently re-parses `structuredContent` against the same schema.\n // A non-idempotent output schema (a field-level `.transform()`) yields data\n // that fails that second parse, which the SDK raises as an opaque protocol\n // error. Detect it here and degrade to a clear isError result (SDK-exempt)\n // instead - structured contracts must be idempotent (no transforms).\n if (!action.output!.safeParse(parsed.data).success) {\n return errorToolResult(new SilkweaveError(\n `Output schema for '${action.name}' is not idempotent (a field-level .transform()?) and cannot back a 'structured' contract - use disposition 'json' or remove the transform.`,\n 'output_schema_not_structurable'\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 // Actual UTF-8 byte count (String.length counts UTF-16 code units, which\n // understates multibyte payloads). TextEncoder keeps this edge-safe.\n resultBytes: new TextEncoder().encode(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,M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text
26
26
  }] };
27
27
  }
28
+ /**
29
+ * Result formatter for `disposition: 'structured'` actions: the (already
30
+ * schema-parsed) data ships as `structuredContent` with a compact JSON text
31
+ * mirror, per the spec's backwards-compat recommendation. Callers must pass
32
+ * the OUTPUT-SCHEMA-PARSED data, not the raw result - parsing strips extra
33
+ * fields, which is what keeps client-side JSON-Schema validation
34
+ * (`additionalProperties: false`) passing by construction.
35
+ */
36
+ function structuredToolResult(data) {
37
+ return {
38
+ content: [{
39
+ type: "text",
40
+ text: JSON.stringify(data)
41
+ }],
42
+ structuredContent: data
43
+ };
44
+ }
28
45
  function jsonToolResult(data, isError = false) {
29
46
  const result = { content: [{
30
47
  type: "text",
@@ -74,6 +91,6 @@ function parseResourceMessage({ resource }) {
74
91
  return "blob" in resource ? Buffer.from(resource.blob, "base64").toString("utf-8") : resource.text;
75
92
  }
76
93
  //#endregion
77
- export { smartToolResult as a, parseResourceMessage as i, handleToolError as n, jsonToolResult as r, errorToolResult as t };
94
+ export { smartToolResult as a, parseResourceMessage as i, handleToolError as n, structuredToolResult as o, jsonToolResult as r, errorToolResult as t };
78
95
 
79
- //# sourceMappingURL=result-B_9Eo1ey.mjs.map
96
+ //# sourceMappingURL=result-CXgeE8ca.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result-CXgeE8ca.mjs","names":[],"sources":["../src/util/result.ts"],"sourcesContent":["import { EmbeddedResource, type CallToolResult } from '@modelcontextprotocol/sdk/types.js'\nimport { SilkweaveError } from '@silkweave/core'\nimport { randomUUID } from 'node:crypto'\n\nexport function smartToolResult(data: string | object | object[]): CallToolResult {\n const text = typeof data === 'string' ? data : JSON.stringify(data)\n const mimeType = typeof data === 'string' ? 'text/plain' : 'application/json'\n const ext = typeof data === 'string' ? 'txt' : 'json'\n if (text.length > 4096) {\n const uri = `mcp://toolResult/${randomUUID()}.${ext}`\n const buffer = Buffer.from(text)\n const blob = buffer.toString('base64')\n return {\n content: [\n { type: 'text', text: `Received resource ${uri} with ${buffer.byteLength} bytes` },\n { type: 'resource', resource: { uri, mimeType, blob } }\n ]\n }\n } else {\n return {\n content: [{ type: 'text' as const, text }]\n }\n }\n}\n\n/**\n * Result formatter for `disposition: 'structured'` actions: the (already\n * schema-parsed) data ships as `structuredContent` with a compact JSON text\n * mirror, per the spec's backwards-compat recommendation. Callers must pass\n * the OUTPUT-SCHEMA-PARSED data, not the raw result - parsing strips extra\n * fields, which is what keeps client-side JSON-Schema validation\n * (`additionalProperties: false`) passing by construction.\n */\nexport function structuredToolResult(data: object): CallToolResult {\n return {\n content: [{ type: 'text' as const, text: JSON.stringify(data) }],\n structuredContent: data as Record<string, unknown>\n }\n}\n\nexport function jsonToolResult(data: object, isError = false): CallToolResult {\n const result: CallToolResult = { content: [{ type: 'text' as const, text: JSON.stringify(data) }] }\n if (isError) { result.isError = true }\n return result\n}\n\nexport function errorToolResult({ code, name, message }: SilkweaveError): CallToolResult {\n return {\n isError: true,\n content: [{\n type: 'text',\n text: JSON.stringify({ success: false, code, name, message })\n }]\n }\n}\n\nexport function handleToolError(error: unknown): CallToolResult {\n if (error instanceof SilkweaveError) {\n return jsonToolResult({ success: false, name: error.name, message: error.message, code: error.code }, true)\n } else if (error instanceof Error) {\n // Log the full error (incl. stack) to stderr only - never put the stack trace\n // on the wire, where it would leak server internals to the MCP client.\n console.error(error)\n return jsonToolResult({ success: false, name: error.name, message: error.message }, true)\n } else {\n // Likewise keep the raw thrown value server-side - only a generic message goes out.\n console.error('Unknown tool error:', error)\n return jsonToolResult({ success: false, name: 'Unknown error', message: 'An unknown error occurred' }, true)\n }\n}\n\nexport function parseResourceMessage({ resource }: EmbeddedResource) {\n const text = ('blob' in resource) ? Buffer.from(resource.blob, 'base64').toString('utf-8') : resource.text\n return text\n}\n"],"mappings":";;;AAIA,SAAgB,gBAAgB,MAAkD;CAChF,MAAM,OAAO,OAAO,SAAS,WAAW,OAAO,KAAK,UAAU,KAAK;CACnE,MAAM,WAAW,OAAO,SAAS,WAAW,eAAe;CAC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AAM,uBAAuB,MAAM;AAC3C,SAAO,eAAe;GAAE,SAAS;GAAO,MAAM;GAAiB,SAAS;GAA6B,EAAE,KAAK;;;AAIhH,SAAgB,qBAAqB,EAAE,YAA8B;AAEnE,QADc,UAAU,WAAY,OAAO,KAAK,SAAS,MAAM,SAAS,CAAC,SAAS,QAAQ,GAAG,SAAS"}
@@ -0,0 +1,136 @@
1
+ import { Action, OnToolCall, SilkweaveContext, SilkweaveError, createLogger } from "@silkweave/core";
2
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
+ import { CallToolResult, EmbeddedResource } from "@modelcontextprotocol/sdk/types.js";
4
+
5
+ //#region src/handlers/filter.d.ts
6
+ /**
7
+ * Normalized request stand-in handed to `filterActions` - the same shape for
8
+ * the Express `http()` transport and the Web-Standard `edge()` adapter.
9
+ */
10
+ interface FilterRequest {
11
+ /** Inbound HTTP headers (lower-cased keys, as delivered by the host). */
12
+ headers: Record<string, string | string[] | undefined>;
13
+ /** Full request URL (or path, as delivered by the host). */
14
+ url: string;
15
+ /**
16
+ * JSON-RPC method of the POSTed message (`'initialize'`, `'tools/list'`,
17
+ * `'tools/call'`, `'ping'`, ...). Lets the callback skip expensive
18
+ * permission lookups on `initialize`/`ping`, and doubles as an
19
+ * observability tap (e.g. counting `tools/list`). Empty string when the body
20
+ * is not a recognizable JSON-RPC message. JSON-RPC batches are rejected by the
21
+ * transport before the filter runs, so this always reflects a single message.
22
+ */
23
+ method: string;
24
+ /** `params.name` of a `tools/call` message; unset for every other method. */
25
+ toolName?: string;
26
+ }
27
+ /**
28
+ * Per-request tool filter for the stateless MCP transports. Runs before
29
+ * `registerTools()` on every `POST /mcp`; only the returned actions exist for
30
+ * that request (`tools/list` and `tools/call` alike - a client that cached a
31
+ * wider list is still denied). May be async (e.g. a DB lookup of API-key
32
+ * permissions).
33
+ *
34
+ * Error semantics: a thrown `SilkweaveError` propagates as its `statusCode`
35
+ * (e.g. 401 invalid key, 403 insufficient permissions) with a JSON-RPC error
36
+ * body; any other throw maps to HTTP 500. A thrown error NEVER degrades to an
37
+ * empty tool list - return `[]` explicitly if "no tools" is the intended
38
+ * answer.
39
+ */
40
+ type FilterActions = (actions: Action[], request: FilterRequest) => Action[] | Promise<Action[]>;
41
+ /**
42
+ * Extract the JSON-RPC `method` (and `toolName` for `tools/call`) from a
43
+ * parsed request body. Tolerates a legacy batch (first request wins) and
44
+ * unrecognizable bodies (empty `method`).
45
+ */
46
+ declare function rpcInfo(body: unknown): {
47
+ method: string;
48
+ toolName?: string;
49
+ };
50
+ /**
51
+ * Map a `filterActions` throw to an HTTP status + JSON-RPC error body: a
52
+ * `SilkweaveError` keeps its `statusCode` (so an invalid key surfaces as an
53
+ * auth failure, not "server has no tools"), anything else is a 500. `id` is
54
+ * echoed from the request body when available.
55
+ */
56
+ declare function filterErrorResponse(error: unknown, body: unknown): {
57
+ status: number;
58
+ body: object;
59
+ };
60
+ //#endregion
61
+ //#region src/handlers/registerTools.d.ts
62
+ type LogStream = NonNullable<Parameters<typeof createLogger>[0]>['stream'];
63
+ interface RegisterToolsOptions {
64
+ /**
65
+ * Where the per-call logger writes its human-readable stream. The `stdio`
66
+ * adapter MUST pass `false` (stdout is the MCP protocol channel); the HTTP
67
+ * transports default to `process.stderr`.
68
+ */
69
+ logStream?: LogStream;
70
+ /**
71
+ * Telemetry hook invoked once per tool call (fire-and-forget; errors are
72
+ * logged, never propagated). Fires after result formatting, so events carry
73
+ * `resultBytes` (serialized raw-result size) and `sideloaded` (whether
74
+ * `smartToolResult` offloaded to an embedded resource).
75
+ */
76
+ onToolCall?: OnToolCall;
77
+ }
78
+ /**
79
+ * Build the generic `request` context value (the same key REST/tRPC populate)
80
+ * from the MCP SDK's `extra.requestInfo`, so transport-agnostic consumers - e.g.
81
+ * `@silkweave/nestjs` `@UseGuards` guards reading
82
+ * `switchToHttp().getRequest().headers` - work over MCP. There are no path
83
+ * `params`/`query`/`body` on the raw MCP call, so those start as empty
84
+ * stand-ins; `@silkweave/nestjs` later fills them from the validated tool input
85
+ * per the reflected param sources (path -> `params`, `@Query` -> `query`,
86
+ * `@Body` -> `body`) so guards reading any of them decide as they would over
87
+ * REST. Returns `undefined` when no HTTP request info is available.
88
+ */
89
+ declare function requestFromExtra(requestInfo: {
90
+ headers?: unknown;
91
+ url?: {
92
+ toString(): string;
93
+ };
94
+ } | undefined): {
95
+ headers: {};
96
+ url: string | undefined;
97
+ params: {};
98
+ query: {};
99
+ body: {};
100
+ } | undefined;
101
+ /**
102
+ * Register every action as an MCP tool on `server`. Shared by all MCP transports
103
+ * (`stdio`, `http`, `edge`). Each tool call forks `context` with a per-call
104
+ * `logger`, the SDK `extra`, a synthesized `request` (see `requestFromExtra`),
105
+ * and - when bearer auth ran for this request - the resolved `auth`. The result
106
+ * is formatted by the action's `toolResult` hook if present, otherwise per the
107
+ * resolved disposition (client `_meta.disposition` > `action.disposition` >
108
+ * `json`); a `'structured'` action always ships schema-parsed
109
+ * `structuredContent` (its contract cannot be demoted per-call).
110
+ */
111
+ declare function registerTools(server: McpServer, actions: Action[], context: SilkweaveContext, options?: RegisterToolsOptions): void;
112
+ //#endregion
113
+ //#region src/util/result.d.ts
114
+ declare function smartToolResult(data: string | object | object[]): CallToolResult;
115
+ /**
116
+ * Result formatter for `disposition: 'structured'` actions: the (already
117
+ * schema-parsed) data ships as `structuredContent` with a compact JSON text
118
+ * mirror, per the spec's backwards-compat recommendation. Callers must pass
119
+ * the OUTPUT-SCHEMA-PARSED data, not the raw result - parsing strips extra
120
+ * fields, which is what keeps client-side JSON-Schema validation
121
+ * (`additionalProperties: false`) passing by construction.
122
+ */
123
+ declare function structuredToolResult(data: object): CallToolResult;
124
+ declare function jsonToolResult(data: object, isError?: boolean): CallToolResult;
125
+ declare function errorToolResult({
126
+ code,
127
+ name,
128
+ message
129
+ }: SilkweaveError): CallToolResult;
130
+ declare function handleToolError(error: unknown): CallToolResult;
131
+ declare function parseResourceMessage({
132
+ resource
133
+ }: EmbeddedResource): string;
134
+ //#endregion
135
+ export { smartToolResult as a, registerTools as c, FilterRequest as d, filterErrorResponse as f, parseResourceMessage as i, requestFromExtra as l, handleToolError as n, structuredToolResult as o, rpcInfo as p, jsonToolResult as r, RegisterToolsOptions as s, errorToolResult as t, FilterActions as u };
136
+ //# sourceMappingURL=result-I04HIJR5.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result-I04HIJR5.d.mts","names":[],"sources":["../src/handlers/filter.ts","../src/handlers/registerTools.ts","../src/util/result.ts"],"mappings":";;;;;;;;;UAMiB,aAAA;EAAa;EAE5B,OAAA,EAAS,MAAA;EAAM;EAEf,GAAA;EAFS;;;;;;AA6BX;;EAlBE,MAAA;EAkBoC;EAhBpC,QAAA;AAAA;;;;;;;;;;;;;;KAgBU,aAAA,IAAiB,OAAA,EAAS,MAAA,IAAU,OAAA,EAAS,aAAA,KAAkB,MAAA,KAAW,OAAA,CAAQ,MAAA;;;;;;iBAO9E,OAAA,CAAQ,IAAA;EAAkB,MAAA;EAAgB,QAAA;AAAA;;;;;;;iBAa1C,mBAAA,CAAoB,KAAA,WAAgB,IAAA;EAAkB,MAAA;EAAgB,IAAA;AAAA;;;KCjDjF,SAAA,GAAY,WAAA,CAAY,UAAA,QAAkB,YAAA;AAAA,UAG9B,oBAAA;;ADLjB;;;;ECWE,SAAA,GAAY,SAAA;EDTH;;;;;;ECgBT,UAAA,GAAa,UAAA;AAAA;;;;;;;;;;;;iBAcC,gBAAA,CAAiB,WAAA;EAAe,OAAA;EAAmB,GAAA;IAAQ,QAAA;EAAA;AAAA;;;;;;;;;;ADmB3E;;;;;;;iBC0FgB,aAAA,CACd,MAAA,EAAQ,SAAA,EACR,OAAA,EAAS,MAAA,IACT,OAAA,EAAS,gBAAA,EACT,OAAA,GAAS,oBAAA;;;iBCnJK,eAAA,CAAgB,IAAA,+BAAmC,cAAA;;;AFEnE;;;;;;iBE2BgB,oBAAA,CAAqB,IAAA,WAAe,cAAA;AAAA,iBAOpC,cAAA,CAAe,IAAA,UAAc,OAAA,aAAkB,cAAA;AAAA,iBAM/C,eAAA,CAAA;EAAkB,IAAA;EAAM,IAAA;EAAM;AAAA,GAAW,cAAA,GAAiB,cAAA;AAAA,iBAU1D,eAAA,CAAgB,KAAA,YAAiB,cAAA;AAAA,iBAejC,oBAAA,CAAA;EAAuB;AAAA,GAAY,gBAAA"}
package/build/tools.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { a as smartToolResult, c as requestFromExtra, i as parseResourceMessage, n as handleToolError, o as RegisterToolsOptions, r as jsonToolResult, s as registerTools, t as errorToolResult } from "./result-_Bg2cDAG.mjs";
2
- export { RegisterToolsOptions, errorToolResult, handleToolError, jsonToolResult, parseResourceMessage, registerTools, requestFromExtra, smartToolResult };
1
+ import { a as smartToolResult, c as registerTools, d as FilterRequest, f as filterErrorResponse, i as parseResourceMessage, l as requestFromExtra, n as handleToolError, o as structuredToolResult, p as rpcInfo, r as jsonToolResult, s as RegisterToolsOptions, t as errorToolResult, u as FilterActions } from "./result-I04HIJR5.mjs";
2
+ 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 { n as requestFromExtra, t as registerTools } from "./registerTools-Z6JnoM60.mjs";
2
- import { a as smartToolResult, i as parseResourceMessage, n as handleToolError, r as jsonToolResult, t as errorToolResult } from "./result-B_9Eo1ey.mjs";
3
- export { errorToolResult, handleToolError, jsonToolResult, parseResourceMessage, registerTools, requestFromExtra, smartToolResult };
1
+ import { i as rpcInfo, n as requestFromExtra, r as filterErrorResponse, t as registerTools } from "./registerTools-B_vjxOrs.mjs";
2
+ import { a as smartToolResult, i as parseResourceMessage, n as handleToolError, o as structuredToolResult, r as jsonToolResult, t as errorToolResult } from "./result-CXgeE8ca.mjs";
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.1.0",
3
+ "version": "3.2.1",
4
4
  "description": "Silkweave MCP Package",
5
5
  "license": "MIT",
6
6
  "homepage": "https://www.silkweave.dev",
@@ -39,8 +39,8 @@
39
39
  "@modelcontextprotocol/sdk": "^1.29.0",
40
40
  "change-case": "^5.4.4",
41
41
  "zod": "^3.25.0",
42
- "@silkweave/auth": "3.1.0",
43
- "@silkweave/core": "3.1.0"
42
+ "@silkweave/auth": "3.2.1",
43
+ "@silkweave/core": "3.2.1"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "commander": "^13.1.0",
@@ -1,144 +0,0 @@
1
- import { a as smartToolResult, n as handleToolError, r as jsonToolResult } from "./result-B_9Eo1ey.mjs";
2
- import { createLogger, isStreamingAction, runStreamingAction } from "@silkweave/core";
3
- import { validateToken } from "@silkweave/auth";
4
- import { AsyncLocalStorage } from "node:async_hooks";
5
- import { capitalCase, pascalCase } from "change-case";
6
- //#region src/handlers/auth.ts
7
- /**
8
- * Per-request bearer-token storage used by tool handlers to read the resolved
9
- * `AuthInfo` for the currently-handled MCP call.
10
- */
11
- const authStorage = new AsyncLocalStorage();
12
- /**
13
- * Express middleware that validates the `Authorization: Bearer …` header via
14
- * the supplied `AuthConfig`. On success the resolved `AuthInfo` is placed in
15
- * `authStorage` for the duration of the downstream handler - `mcpTransport`'s
16
- * tool callbacks pick it up to populate the silkweave context's `auth` key.
17
- *
18
- * The middleware should NOT be applied to OAuth-discovery / token routes -
19
- * compose it only on the routes that require an authenticated caller (the MCP
20
- * transport itself, sideload, etc.).
21
- */
22
- function authMiddleware(auth, context) {
23
- return async (req, res, next) => {
24
- const result = await validateToken(req.headers.authorization, auth, context.fork({ request: req }));
25
- if (result.error) {
26
- for (const [key, value] of Object.entries(result.error.headers)) res.header(key, value);
27
- res.status(result.error.statusCode).json(result.error.body);
28
- return;
29
- }
30
- if (result.auth) authStorage.run(result.auth, () => {
31
- next();
32
- });
33
- else next();
34
- };
35
- }
36
- //#endregion
37
- //#region src/handlers/registerTools.ts
38
- /**
39
- * Build the generic `request` context value (the same key REST/tRPC populate)
40
- * from the MCP SDK's `extra.requestInfo`, so transport-agnostic consumers - e.g.
41
- * `@silkweave/nestjs` `@UseGuards` guards reading
42
- * `switchToHttp().getRequest().headers` - work over MCP. There are no path
43
- * `params`/`query`/`body` on the raw MCP call, so those start as empty
44
- * stand-ins; `@silkweave/nestjs` later fills them from the validated tool input
45
- * per the reflected param sources (path -> `params`, `@Query` -> `query`,
46
- * `@Body` -> `body`) so guards reading any of them decide as they would over
47
- * REST. Returns `undefined` when no HTTP request info is available.
48
- */
49
- function requestFromExtra(requestInfo) {
50
- if (!requestInfo) return;
51
- return {
52
- headers: requestInfo.headers ?? {},
53
- url: requestInfo.url?.toString(),
54
- params: {},
55
- query: {},
56
- body: {}
57
- };
58
- }
59
- /** Per-call logger that bridges silkweave logs/progress onto MCP notifications. */
60
- function createToolLogger(extra, stream) {
61
- return createLogger({
62
- stream,
63
- onLog: (level, data) => {
64
- extra.sendNotification({
65
- method: "notifications/message",
66
- params: {
67
- level,
68
- data
69
- }
70
- });
71
- },
72
- onProgress: ({ progress, total, message }) => {
73
- if (!extra._meta?.progressToken) return;
74
- extra.sendNotification({
75
- method: "notifications/progress",
76
- params: {
77
- progress,
78
- total,
79
- message,
80
- progressToken: extra._meta.progressToken
81
- }
82
- });
83
- }
84
- });
85
- }
86
- /** Run the action, streaming chunks as progress notifications when a token is set. */
87
- async function runAction(action, input, context, extra) {
88
- const progressToken = extra._meta?.progressToken;
89
- if (isStreamingAction(action)) return runStreamingAction(action, input, context, progressToken ? async (chunk, index) => {
90
- await extra.sendNotification({
91
- method: "notifications/progress",
92
- params: {
93
- progressToken,
94
- progress: index + 1,
95
- message: JSON.stringify(chunk)
96
- }
97
- });
98
- } : void 0);
99
- return action.run(input, context);
100
- }
101
- /** Format via the action's `toolResult` hook, else the resolved disposition. */
102
- function formatToolResult(action, result, context, disposition) {
103
- if (action.toolResult) {
104
- const response = action.toolResult(result, context);
105
- if (response) return response;
106
- }
107
- return disposition === "json" ? jsonToolResult(result) : smartToolResult(result);
108
- }
109
- /**
110
- * Register every action as an MCP tool on `server`. Shared by all MCP transports
111
- * (`stdio`, `http`, `edge`). Each tool call forks `context` with a per-call
112
- * `logger`, the SDK `extra`, a synthesized `request` (see `requestFromExtra`),
113
- * and - when bearer auth ran for this request - the resolved `auth`. The result
114
- * is formatted by the action's `toolResult` hook if present, otherwise per the
115
- * resolved disposition (client `_meta.disposition` > `action.disposition` >
116
- * `smart`).
117
- */
118
- function registerTools(server, actions, context, options = {}) {
119
- const stream = options.logStream ?? process.stderr;
120
- for (const action of actions) server.registerTool(pascalCase(action.name), {
121
- title: capitalCase(action.name),
122
- description: action.description,
123
- inputSchema: action.input
124
- }, async (input, extra) => {
125
- const logger = createToolLogger(extra, stream);
126
- const currentAuth = authStorage.getStore();
127
- const actionContext = context.fork({
128
- logger,
129
- extra,
130
- request: requestFromExtra(extra.requestInfo),
131
- ...currentAuth ? { auth: currentAuth } : {}
132
- });
133
- const disposition = extra._meta?.disposition ?? action.disposition;
134
- try {
135
- return formatToolResult(action, await runAction(action, input, actionContext, extra), actionContext, disposition);
136
- } catch (error) {
137
- return handleToolError(error);
138
- }
139
- });
140
- }
141
- //#endregion
142
- export { authStorage as i, requestFromExtra as n, authMiddleware as r, registerTools as t };
143
-
144
- //# sourceMappingURL=registerTools-Z6JnoM60.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"registerTools-Z6JnoM60.mjs","names":[],"sources":["../src/handlers/auth.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 { 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, isStreamingAction, runStreamingAction, SilkweaveContext } from '@silkweave/core'\nimport { capitalCase, pascalCase } from 'change-case'\nimport { handleToolError, jsonToolResult, smartToolResult } 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\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/** 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 return disposition === 'json' ? jsonToolResult(result) : smartToolResult(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 * `smart`).\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 }, 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 (`smart` when neither is set).\n const disposition = extra._meta?.disposition ?? action.disposition\n try {\n const result = await runAction(action, input, actionContext, extra)\n return formatToolResult(action, result, actionContext, disposition)\n } catch (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;;;;;;;;;;;;;;;;ACDZ,SAAgB,iBAAiB,aAA8E;AAC7G,KAAI,CAAC,YAAe;AACpB,QAAO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,OAAO,YAAY,OAAO,KAAK;EAC/B,aAAa,OAAO;EACpB,aAAa,OAAO;EACrB,EAAE,OAAO,OAAO,UAAU;EACzB,MAAM,SAAS,iBAAiB,OAAO,OAAO;EAC9C,MAAM,cAAc,YAAY,UAAU;EAC1C,MAAM,gBAAgB,QAAQ,KAAK;GACjC;GACA;GACA,SAAS,iBAAiB,MAAM,YAAY;GAC5C,GAAI,cAAc,EAAE,MAAM,aAAa,GAAG,EAAE;GAC7C,CAAC;EAGF,MAAM,cAAc,MAAM,OAAO,eAAe,OAAO;AACvD,MAAI;AAEF,UAAO,iBAAiB,QAAQ,MADX,UAAU,QAAQ,OAAO,eAAe,MAAM,EAC3B,eAAe,YAAY;WAC5D,OAAO;AACd,UAAO,gBAAgB,MAAM;;GAE/B"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"result-B_9Eo1ey.mjs","names":[],"sources":["../src/util/result.ts"],"sourcesContent":["import { EmbeddedResource, type CallToolResult } from '@modelcontextprotocol/sdk/types.js'\nimport { SilkweaveError } from '@silkweave/core'\nimport { randomUUID } from 'node:crypto'\n\nexport function smartToolResult(data: string | object | object[]): CallToolResult {\n const text = typeof data === 'string' ? data : JSON.stringify(data)\n const mimeType = typeof data === 'string' ? 'text/plain' : 'application/json'\n const ext = typeof data === 'string' ? 'txt' : 'json'\n if (text.length > 4096) {\n const uri = `mcp://toolResult/${randomUUID()}.${ext}`\n const buffer = Buffer.from(text)\n const blob = buffer.toString('base64')\n return {\n content: [\n { type: 'text', text: `Received resource ${uri} with ${buffer.byteLength} bytes` },\n { type: 'resource', resource: { uri, mimeType, blob } }\n ]\n }\n } else {\n return {\n content: [{ type: 'text' as const, text }]\n }\n }\n}\n\nexport function jsonToolResult(data: object, isError = false): CallToolResult {\n const result: CallToolResult = { content: [{ type: 'text' as const, text: JSON.stringify(data) }] }\n if (isError) { result.isError = true }\n return result\n}\n\nexport function errorToolResult({ code, name, message }: SilkweaveError): CallToolResult {\n return {\n isError: true,\n content: [{\n type: 'text',\n text: JSON.stringify({ success: false, code, name, message })\n }]\n }\n}\n\nexport function handleToolError(error: unknown): CallToolResult {\n if (error instanceof SilkweaveError) {\n return jsonToolResult({ success: false, name: error.name, message: error.message, code: error.code }, true)\n } else if (error instanceof Error) {\n // Log the full error (incl. stack) to stderr only - never put the stack trace\n // on the wire, where it would leak server internals to the MCP client.\n console.error(error)\n return jsonToolResult({ success: false, name: error.name, message: error.message }, true)\n } else {\n // Likewise keep the raw thrown value server-side - only a generic message goes out.\n console.error('Unknown tool error:', error)\n return jsonToolResult({ success: false, name: 'Unknown error', message: 'An unknown error occurred' }, true)\n }\n}\n\nexport function parseResourceMessage({ resource }: EmbeddedResource) {\n const text = ('blob' in resource) ? Buffer.from(resource.blob, 'base64').toString('utf-8') : resource.text\n return text\n}\n"],"mappings":";;;AAIA,SAAgB,gBAAgB,MAAkD;CAChF,MAAM,OAAO,OAAO,SAAS,WAAW,OAAO,KAAK,UAAU,KAAK;CACnE,MAAM,WAAW,OAAO,SAAS,WAAW,eAAe;CAC3D,MAAM,MAAM,OAAO,SAAS,WAAW,QAAQ;AAC/C,KAAI,KAAK,SAAS,MAAM;EACtB,MAAM,MAAM,oBAAoB,YAAY,CAAC,GAAG;EAChD,MAAM,SAAS,OAAO,KAAK,KAAK;EAChC,MAAM,OAAO,OAAO,SAAS,SAAS;AACtC,SAAO,EACL,SAAS,CACP;GAAE,MAAM;GAAQ,MAAM,qBAAqB,IAAI,QAAQ,OAAO,WAAW;GAAS,EAClF;GAAE,MAAM;GAAY,UAAU;IAAE;IAAK;IAAU;IAAM;GAAE,CACxD,EACF;OAED,QAAO,EACL,SAAS,CAAC;EAAE,MAAM;EAAiB;EAAM,CAAC,EAC3C;;AAIL,SAAgB,eAAe,MAAc,UAAU,OAAuB;CAC5E,MAAM,SAAyB,EAAE,SAAS,CAAC;EAAE,MAAM;EAAiB,MAAM,KAAK,UAAU,KAAK;EAAE,CAAC,EAAE;AACnG,KAAI,QAAW,QAAO,UAAU;AAChC,QAAO;;AAGT,SAAgB,gBAAgB,EAAE,MAAM,MAAM,WAA2C;AACvF,QAAO;EACL,SAAS;EACT,SAAS,CAAC;GACR,MAAM;GACN,MAAM,KAAK,UAAU;IAAE,SAAS;IAAO;IAAM;IAAM;IAAS,CAAC;GAC9D,CAAC;EACH;;AAGH,SAAgB,gBAAgB,OAAgC;AAC9D,KAAI,iBAAiB,eACnB,QAAO,eAAe;EAAE,SAAS;EAAO,MAAM,MAAM;EAAM,SAAS,MAAM;EAAS,MAAM,MAAM;EAAM,EAAE,KAAK;UAClG,iBAAiB,OAAO;AAGjC,UAAQ,MAAM,MAAM;AACpB,SAAO,eAAe;GAAE,SAAS;GAAO,MAAM,MAAM;GAAM,SAAS,MAAM;GAAS,EAAE,KAAK;QACpF;AAEL,UAAQ,MAAM,uBAAuB,MAAM;AAC3C,SAAO,eAAe;GAAE,SAAS;GAAO,MAAM;GAAiB,SAAS;GAA6B,EAAE,KAAK;;;AAIhH,SAAgB,qBAAqB,EAAE,YAA8B;AAEnE,QADc,UAAU,WAAY,OAAO,KAAK,SAAS,MAAM,SAAS,CAAC,SAAS,QAAQ,GAAG,SAAS"}
@@ -1,63 +0,0 @@
1
- import { Action, SilkweaveContext, SilkweaveError, createLogger } from "@silkweave/core";
2
- import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
- import { CallToolResult, EmbeddedResource } from "@modelcontextprotocol/sdk/types.js";
4
-
5
- //#region src/handlers/registerTools.d.ts
6
- type LogStream = NonNullable<Parameters<typeof createLogger>[0]>['stream'];
7
- interface RegisterToolsOptions {
8
- /**
9
- * Where the per-call logger writes its human-readable stream. The `stdio`
10
- * adapter MUST pass `false` (stdout is the MCP protocol channel); the HTTP
11
- * transports default to `process.stderr`.
12
- */
13
- logStream?: LogStream;
14
- }
15
- /**
16
- * Build the generic `request` context value (the same key REST/tRPC populate)
17
- * from the MCP SDK's `extra.requestInfo`, so transport-agnostic consumers - e.g.
18
- * `@silkweave/nestjs` `@UseGuards` guards reading
19
- * `switchToHttp().getRequest().headers` - work over MCP. There are no path
20
- * `params`/`query`/`body` on the raw MCP call, so those start as empty
21
- * stand-ins; `@silkweave/nestjs` later fills them from the validated tool input
22
- * per the reflected param sources (path -> `params`, `@Query` -> `query`,
23
- * `@Body` -> `body`) so guards reading any of them decide as they would over
24
- * REST. Returns `undefined` when no HTTP request info is available.
25
- */
26
- declare function requestFromExtra(requestInfo: {
27
- headers?: unknown;
28
- url?: {
29
- toString(): string;
30
- };
31
- } | undefined): {
32
- headers: {};
33
- url: string | undefined;
34
- params: {};
35
- query: {};
36
- body: {};
37
- } | undefined;
38
- /**
39
- * Register every action as an MCP tool on `server`. Shared by all MCP transports
40
- * (`stdio`, `http`, `edge`). Each tool call forks `context` with a per-call
41
- * `logger`, the SDK `extra`, a synthesized `request` (see `requestFromExtra`),
42
- * and - when bearer auth ran for this request - the resolved `auth`. The result
43
- * is formatted by the action's `toolResult` hook if present, otherwise per the
44
- * resolved disposition (client `_meta.disposition` > `action.disposition` >
45
- * `smart`).
46
- */
47
- declare function registerTools(server: McpServer, actions: Action[], context: SilkweaveContext, options?: RegisterToolsOptions): void;
48
- //#endregion
49
- //#region src/util/result.d.ts
50
- declare function smartToolResult(data: string | object | object[]): CallToolResult;
51
- declare function jsonToolResult(data: object, isError?: boolean): CallToolResult;
52
- declare function errorToolResult({
53
- code,
54
- name,
55
- message
56
- }: SilkweaveError): CallToolResult;
57
- declare function handleToolError(error: unknown): CallToolResult;
58
- declare function parseResourceMessage({
59
- resource
60
- }: EmbeddedResource): string;
61
- //#endregion
62
- export { smartToolResult as a, requestFromExtra as c, parseResourceMessage as i, handleToolError as n, RegisterToolsOptions as o, jsonToolResult as r, registerTools as s, errorToolResult as t };
63
- //# sourceMappingURL=result-_Bg2cDAG.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"result-_Bg2cDAG.d.mts","names":[],"sources":["../src/handlers/registerTools.ts","../src/util/result.ts"],"mappings":";;;;;KAQK,SAAA,GAAY,WAAA,CAAY,UAAA,QAAkB,YAAA;AAAA,UAG9B,oBAAA;;AARyG;;;;EAcxH,SAAA,GAAY,SAAA;AAAA;;;;;;;;AANd;;;;iBAoBgB,gBAAA,CAAiB,WAAA;EAAe,OAAA;EAAmB,GAAA;IAAQ,QAAA;EAAA;AAAA;;;;;;;;;;;AAwD3E;;;;;iBAAgB,aAAA,CACd,MAAA,EAAQ,SAAA,EACR,OAAA,EAAS,MAAA,IACT,OAAA,EAAS,gBAAA,EACT,OAAA,GAAS,oBAAA;;;iBCvFK,eAAA,CAAgB,IAAA,+BAAmC,cAAA;AAAA,iBAqBnD,cAAA,CAAe,IAAA,UAAc,OAAA,aAAkB,cAAA;AAAA,iBAM/C,eAAA,CAAA;EAAkB,IAAA;EAAM,IAAA;EAAM;AAAA,GAAW,cAAA,GAAiB,cAAA;AAAA,iBAU1D,eAAA,CAAgB,KAAA,YAAiB,cAAA;AAAA,iBAejC,oBAAA,CAAA;EAAuB;AAAA,GAAY,gBAAA"}