@softure-ai/mcp-access 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +32 -1
  3. package/dist/index.js +1 -1
  4. package/dist/jobs/index.d.ts +1 -1
  5. package/dist/jobs/index.d.ts.map +1 -1
  6. package/dist/jobs/index.js +1 -1
  7. package/dist/jobs/index.js.map +1 -1
  8. package/dist/jobs/operation-tasks.d.ts +11 -4
  9. package/dist/jobs/operation-tasks.d.ts.map +1 -1
  10. package/dist/jobs/operation-tasks.js +5 -3
  11. package/dist/jobs/operation-tasks.js.map +1 -1
  12. package/dist/jobs/operations.d.ts +28 -7
  13. package/dist/jobs/operations.d.ts.map +1 -1
  14. package/dist/jobs/operations.js +27 -6
  15. package/dist/jobs/operations.js.map +1 -1
  16. package/dist/server/endpoint.d.ts +13 -2
  17. package/dist/server/endpoint.d.ts.map +1 -1
  18. package/dist/server/endpoint.js +35 -2
  19. package/dist/server/endpoint.js.map +1 -1
  20. package/dist/server/index.d.ts +2 -1
  21. package/dist/server/index.d.ts.map +1 -1
  22. package/dist/server/index.js +1 -0
  23. package/dist/server/index.js.map +1 -1
  24. package/dist/server/server-factory.d.ts +7 -3
  25. package/dist/server/server-factory.d.ts.map +1 -1
  26. package/dist/server/server-factory.js +9 -3
  27. package/dist/server/server-factory.js.map +1 -1
  28. package/dist/server/tool-list-session.d.ts +22 -0
  29. package/dist/server/tool-list-session.d.ts.map +1 -0
  30. package/dist/server/tool-list-session.js +97 -0
  31. package/dist/server/tool-list-session.js.map +1 -0
  32. package/dist/testing/index.d.ts +1 -0
  33. package/dist/testing/index.d.ts.map +1 -1
  34. package/dist/testing/index.js +1 -0
  35. package/dist/testing/index.js.map +1 -1
  36. package/dist/testing/sessions.d.ts +8 -0
  37. package/dist/testing/sessions.d.ts.map +1 -0
  38. package/dist/testing/sessions.js +40 -0
  39. package/dist/testing/sessions.js.map +1 -0
  40. package/module.json +1 -1
  41. package/package.json +1 -1
  42. package/src/index.ts +1 -1
  43. package/src/jobs/index.ts +2 -0
  44. package/src/jobs/operation-tasks.ts +12 -7
  45. package/src/jobs/operations.ts +62 -17
  46. package/src/server/endpoint.ts +57 -3
  47. package/src/server/index.ts +8 -0
  48. package/src/server/server-factory.ts +11 -5
  49. package/src/server/tool-list-session.ts +103 -0
  50. package/src/testing/index.ts +1 -0
  51. package/src/testing/sessions.ts +41 -0
@@ -19,6 +19,14 @@ import { getProtectedResourceMetadataUrl, isOAuthEnabled } from "./oauth-http.js
19
19
  import { resolveMcpOrigins } from "./origins.js";
20
20
  import { MCP_READ_SCOPE, MCP_WRITE_SCOPE } from "./scopes.js";
21
21
  import { verifyAccessToken, type McpAccessContext } from "./tokens.js";
22
+ import {
23
+ createSessionRenewalResponse,
24
+ createToolListSessionId,
25
+ isInitializeRequest,
26
+ isToolListSessionCurrent,
27
+ MCP_SESSION_ID_HEADER,
28
+ rewriteInitializeAnswer,
29
+ } from "./tool-list-session.js";
22
30
  import { isWritingEnabled } from "./writes-switch.js";
23
31
 
24
32
  export { MCP_READ_SCOPE, MCP_WRITE_SCOPE };
@@ -37,8 +45,21 @@ const NO_STORE = { "cache-control": "no-store" };
37
45
  /** The app's MCP server for one request. It registers write tools only when `canWrite` is true. */
38
46
  export type McpServerFactory = (identity: McpServerIdentity) => McpServer | Server | Promise<McpServer | Server>;
39
47
 
48
+ /** Reads the tool list version the factory's servers answer at `initialize` for the identity. */
49
+ export type ToolListVersionReader = (identity: McpServerIdentity) => Promise<string>;
50
+
51
+ /** A factory that can tell its tool list version without building a server; `createMcpServerFactory` returns one. */
52
+ export interface VersionedMcpServerFactory extends McpServerFactory {
53
+ readonly toolListVersion: ToolListVersionReader;
54
+ }
55
+
40
56
  export interface McpEndpointOptions {
41
- readonly createServer: McpServerFactory;
57
+ readonly createServer: McpServerFactory | VersionedMcpServerFactory;
58
+ /**
59
+ * The tool list version per identity, for sessions tied to it (#1055). Default: the factory's own
60
+ * `toolListVersion` (`createMcpServerFactory`). Without either, the endpoint issues no session id.
61
+ */
62
+ readonly toolListVersion?: ToolListVersionReader;
42
63
  }
43
64
 
44
65
  /** Serves one request to the MCP endpoint. */
@@ -50,7 +71,8 @@ export type McpEndpoint = (ctx: McpAccessContext, request: Request) => Promise<R
50
71
  * (`responseMode: "json"`): a stream would add proxy buffering and timeouts for no tool that needs
51
72
  * it, and mid-call notifications are dropped.
52
73
  */
53
- export function createMcpEndpoint({ createServer }: McpEndpointOptions): McpEndpoint {
74
+ export function createMcpEndpoint({ createServer, toolListVersion }: McpEndpointOptions): McpEndpoint {
75
+ const readToolListVersion = toolListVersion ?? getFactoryToolListVersion(createServer);
54
76
  const handler = createMcpHandler(
55
77
  (requestContext) => createServer(readIdentity(requestContext.authInfo)),
56
78
  {
@@ -83,8 +105,40 @@ export function createMcpEndpoint({ createServer }: McpEndpointOptions): McpEndp
83
105
 
84
106
  const authInfo = await authenticate(ctx, request);
85
107
  if (authInfo instanceof Response) return authInfo;
86
- return handler.fetch(request, { authInfo });
108
+ return serveInSession(request, authInfo);
87
109
  };
110
+
111
+ /**
112
+ * Sessions tied to the tool list version (#1055): a session from another version (a release changed a tool, or the
113
+ * access level changed) answers 404, so the client initializes again and lists the tools again; `initialize` gets a
114
+ * new session id. A request without a session id is served as it always was.
115
+ */
116
+ async function serveInSession(request: Request, authInfo: AuthInfo): Promise<Response> {
117
+ const sessionId = request.headers.get(MCP_SESSION_ID_HEADER);
118
+ const isInitialize = sessionId === null && (await isInitializeRequest(request, MAX_MCP_REQUEST_BYTES));
119
+ if (readToolListVersion === undefined || (sessionId === null && !isInitialize)) {
120
+ const response = await handler.fetch(request, { authInfo });
121
+ return isInitialize ? rewriteInitializeAnswer(response, null) : response;
122
+ }
123
+ let version;
124
+ try {
125
+ version = await readToolListVersion(readIdentity(authInfo));
126
+ } catch (error) {
127
+ // Without the version no session can be checked or issued: serve the request statelessly, as before #1055.
128
+ console.error(`@softure-ai/mcp-access: reading the tool list version failed: ${errorLogLabel(error)}`);
129
+ const response = await handler.fetch(request, { authInfo });
130
+ return isInitialize ? rewriteInitializeAnswer(response, null) : response;
131
+ }
132
+ if (sessionId !== null) {
133
+ return isToolListSessionCurrent(sessionId, version) ? handler.fetch(request, { authInfo }) : createSessionRenewalResponse();
134
+ }
135
+ return rewriteInitializeAnswer(await handler.fetch(request, { authInfo }), createToolListSessionId(version));
136
+ }
137
+ }
138
+
139
+ /** The factory's own version reader, when it has one (`createMcpServerFactory`). */
140
+ function getFactoryToolListVersion(factory: McpServerFactory | VersionedMcpServerFactory): ToolListVersionReader | undefined {
141
+ return "toolListVersion" in factory && typeof factory.toolListVersion === "function" ? factory.toolListVersion : undefined;
88
142
  }
89
143
 
90
144
  /**
@@ -9,6 +9,8 @@ export {
9
9
  type McpEndpoint,
10
10
  type McpEndpointOptions,
11
11
  type McpServerFactory,
12
+ type ToolListVersionReader,
13
+ type VersionedMcpServerFactory,
12
14
  } from "./endpoint.js";
13
15
  export { actionTool, toFormData, type ActionToolOptions, type FormActionHandler } from "./action-tool.js";
14
16
  export {
@@ -155,6 +157,12 @@ export {
155
157
  type ServedTool,
156
158
  type ToolListMeasurement,
157
159
  } from "./tool-list.js";
160
+ export {
161
+ createToolListSessionId,
162
+ isToolListSessionCurrent,
163
+ MCP_SESSION_ID_HEADER,
164
+ readToolListSessionVersion,
165
+ } from "./tool-list-session.js";
158
166
  export {
159
167
  toolError,
160
168
  toolResult,
@@ -10,7 +10,7 @@
10
10
  // });
11
11
  import { McpServer, type McpServerOptions } from "@modelcontextprotocol/server";
12
12
  import type { McpServerIdentity } from "../contract.js";
13
- import type { McpServerFactory } from "./endpoint.js";
13
+ import type { VersionedMcpServerFactory } from "./endpoint.js";
14
14
  import { withStrictToolInput, type StrictToolInputOptions } from "./strict-input.js";
15
15
  import { registerServerInfoTool, type ServerInfoToolOptions } from "./server-info.js";
16
16
  import { measureToolList, type ToolListMeasurement } from "./tool-list.js";
@@ -39,8 +39,12 @@ export interface McpServerFactoryOptions {
39
39
  readonly serverInfoTool?: ServerInfoToolOptions;
40
40
  }
41
41
 
42
- /** An `McpServerFactory` whose servers carry the tool list's version for the identity's access level. */
43
- export function createMcpServerFactory(options: McpServerFactoryOptions): McpServerFactory {
42
+ /**
43
+ * An `McpServerFactory` whose servers carry the tool list's version for the identity's access level. Its
44
+ * `toolListVersion(identity)` reads that version without building a server, so `createMcpEndpoint` ties each session
45
+ * to it (#1055).
46
+ */
47
+ export function createMcpServerFactory(options: McpServerFactoryOptions): VersionedMcpServerFactory {
44
48
  const strictOptions = options.strictToolInput === true ? {} : options.strictToolInput === false ? undefined : options.strictToolInput;
45
49
  // The served version reaches `get_server_info` here; the recording server registers it with a placeholder, since
46
50
  // its handler never runs there and the version is not part of the listed tool.
@@ -66,8 +70,9 @@ export function createMcpServerFactory(options: McpServerFactoryOptions): McpSer
66
70
  return measurement;
67
71
  };
68
72
 
69
- return async (identity) => {
70
- const version = options.version ?? (await measure(identity)).toolListVersion;
73
+ const toolListVersion = async (identity: McpServerIdentity): Promise<string> => options.version ?? (await measure(identity)).toolListVersion;
74
+ const factory = async (identity: McpServerIdentity): Promise<McpServer> => {
75
+ const version = await toolListVersion(identity);
71
76
  const server = new McpServer(
72
77
  { name: options.name, version, ...(options.title === undefined ? {} : { title: options.title }) },
73
78
  options.serverOptions,
@@ -75,4 +80,5 @@ export function createMcpServerFactory(options: McpServerFactoryOptions): McpSer
75
80
  await registerTools(server, identity, version);
76
81
  return server;
77
82
  };
83
+ return Object.assign(factory, { toolListVersion });
78
84
  }
@@ -0,0 +1,103 @@
1
+ // Sessions tied to the tool list version (#1055). A hosted client keeps its cached `tools/list` for as long as its
2
+ // connection lives, even after a release changed a tool, and the endpoint has no channel to send
3
+ // `notifications/tools/list_changed`. So the endpoint answers `initialize` with an `Mcp-Session-Id` that carries the
4
+ // tool list version of the identity's access level, and a later request whose session carries another version gets
5
+ // `404 Not Found`: Streamable HTTP defines that as "start a new session", so the client initializes again and lists
6
+ // the tools again. Nothing is stored: the id itself holds the version, so any instance can check it.
7
+ import { randomBytes } from "node:crypto";
8
+
9
+ /** The header that carries the session id, both ways. */
10
+ export const MCP_SESSION_ID_HEADER = "mcp-session-id";
11
+
12
+ /** Random bytes after the version, as hex. */
13
+ const RANDOM_BYTES = 16;
14
+ const SESSION_ID_PATTERN = /^([A-Za-z0-9_-]+)\.[0-9a-f]+$/;
15
+ const SSE_DATA_PREFIX = "data: ";
16
+
17
+ /** A new session id for the version: `<base64url(version)>.<32 hex>`, visible ASCII whatever the version holds. */
18
+ export function createToolListSessionId(toolListVersion: string): string {
19
+ return `${Buffer.from(toolListVersion, "utf8").toString("base64url")}.${randomBytes(RANDOM_BYTES).toString("hex")}`;
20
+ }
21
+
22
+ /** The tool list version a session id carries, or `null` when the id is not one the endpoint issues. */
23
+ export function readToolListSessionVersion(sessionId: string): string | null {
24
+ const match = SESSION_ID_PATTERN.exec(sessionId);
25
+ if (match?.[1] === undefined) return null;
26
+ return Buffer.from(match[1], "base64url").toString("utf8");
27
+ }
28
+
29
+ /** Whether a request in this session may be served: its version is the current one. */
30
+ export function isToolListSessionCurrent(sessionId: string, toolListVersion: string): boolean {
31
+ return readToolListSessionVersion(sessionId) === toolListVersion;
32
+ }
33
+
34
+ /** The answer to a request in a session from another tool list version: the client starts a new session. */
35
+ export function createSessionRenewalResponse(): Response {
36
+ return Response.json(
37
+ { jsonrpc: "2.0", error: { code: -32001, message: "Session not found: the tool list changed, initialize again" }, id: null },
38
+ { status: 404, headers: { "cache-control": "no-store" } },
39
+ );
40
+ }
41
+
42
+ /**
43
+ * Whether the request is a JSON-RPC `initialize`: a POST without a session id whose JSON body asks for it. The body
44
+ * is read from a clone, and a body over `maxBytes` is not read (the SDK refuses it with 413 anyway).
45
+ */
46
+ export async function isInitializeRequest(request: Request, maxBytes: number): Promise<boolean> {
47
+ if (request.method !== "POST" || request.headers.has(MCP_SESSION_ID_HEADER)) return false;
48
+ const length = Number(request.headers.get("content-length") ?? "0");
49
+ if (!Number.isFinite(length) || length > maxBytes) return false;
50
+ let body: unknown;
51
+ try {
52
+ const text = await request.clone().text();
53
+ if (text.length > maxBytes) return false;
54
+ body = JSON.parse(text);
55
+ } catch {
56
+ // Not JSON: the SDK answers the parse error itself.
57
+ return false;
58
+ }
59
+ return typeof body === "object" && body !== null && !Array.isArray(body) && (body as { method?: unknown }).method === "initialize";
60
+ }
61
+
62
+ /**
63
+ * The `initialize` answer with `capabilities.tools.listChanged: false`, since no answer of the endpoint ever carries
64
+ * the notification, and with the session id when there is one. The body keeps its format (JSON or one SSE event);
65
+ * an answer that is not a successful `InitializeResult` is returned as it is.
66
+ */
67
+ export async function rewriteInitializeAnswer(response: Response, sessionId: string | null): Promise<Response> {
68
+ if (response.status !== 200) return response;
69
+ const text = await response.text();
70
+ const isEventStream = (response.headers.get("content-type") ?? "").includes("text/event-stream");
71
+ const lines = text.split("\n");
72
+ const dataIndex = isEventStream ? lines.findIndex((line) => line.startsWith(SSE_DATA_PREFIX)) : -1;
73
+ const json = isEventStream ? (dataIndex === -1 ? null : (lines[dataIndex] ?? "").slice(SSE_DATA_PREFIX.length)) : text;
74
+
75
+ const answer = parseInitializeAnswer(json);
76
+ const headers = new Headers(response.headers);
77
+ // The body changes length, so the old one would be wrong.
78
+ headers.delete("content-length");
79
+ const tools = answer?.result?.capabilities?.tools;
80
+ if (answer === null || answer.result?.protocolVersion === undefined) {
81
+ return new Response(text, { status: response.status, statusText: response.statusText, headers });
82
+ }
83
+ if (tools !== undefined && tools !== null && typeof tools === "object") tools.listChanged = false;
84
+ if (sessionId !== null) headers.set(MCP_SESSION_ID_HEADER, sessionId);
85
+
86
+ const rewritten = JSON.stringify(answer);
87
+ if (isEventStream) lines[dataIndex] = `${SSE_DATA_PREFIX}${rewritten}`;
88
+ return new Response(isEventStream ? lines.join("\n") : rewritten, { status: response.status, statusText: response.statusText, headers });
89
+ }
90
+
91
+ /** The answer as JSON, or `null` when there is none or it is not JSON (the SDK's own error text). */
92
+ function parseInitializeAnswer(json: string | null): InitializeAnswer | null {
93
+ if (json === null) return null;
94
+ try {
95
+ return JSON.parse(json) as InitializeAnswer;
96
+ } catch {
97
+ return null;
98
+ }
99
+ }
100
+
101
+ interface InitializeAnswer {
102
+ result?: { protocolVersion?: unknown; capabilities?: { tools?: { listChanged?: boolean } | null } };
103
+ }
@@ -6,3 +6,4 @@ export {
6
6
  type ServedToolLists,
7
7
  type ToolCatalogDifferences,
8
8
  } from "./catalog.js";
9
+ export { expectToolListSessionRenewed } from "./sessions.js";
@@ -0,0 +1,41 @@
1
+ // Sessions tied to the tool list version (#1055), checked on the app's factories: a release that changes a tool must
2
+ // renew the sessions opened before it, or a connected client keeps the old schemas. Framework-free: it throws.
3
+ import type { McpServerIdentity } from "../contract.js";
4
+ import type { McpServerFactory, VersionedMcpServerFactory } from "../server/endpoint.js";
5
+ import { createToolListSessionId, isToolListSessionCurrent } from "../server/tool-list-session.js";
6
+ import { fingerprintToolList, listServedTools } from "../server/tool-list.js";
7
+
8
+ const READER: McpServerIdentity = { userId: "00000000-0000-4000-8000-000000000001", canWrite: false, tokenId: "session-check" };
9
+ const WRITER: McpServerIdentity = { ...READER, canWrite: true };
10
+ const LEVELS = [
11
+ ["read", READER],
12
+ ["write", WRITER],
13
+ ] as const;
14
+
15
+ /**
16
+ * Throws unless a session opened under `before` (the previous release's factory) is renewed under `after` for every
17
+ * access level whose served tool list changed, and kept for every level whose list did not. A pair where no level
18
+ * changed throws too: the check would prove nothing.
19
+ */
20
+ export async function expectToolListSessionRenewed(before: McpServerFactory, after: McpServerFactory): Promise<void> {
21
+ const problems: string[] = [];
22
+ const unchanged: string[] = [];
23
+ for (const [level, identity] of LEVELS) {
24
+ const sessionId = createToolListSessionId(await readVersion(before, identity));
25
+ const isCurrent = isToolListSessionCurrent(sessionId, await readVersion(after, identity));
26
+ const hasChanged = fingerprintToolList(await listServedTools(() => before(identity))) !== fingerprintToolList(await listServedTools(() => after(identity)));
27
+ if (!hasChanged) unchanged.push(level);
28
+ if (hasChanged && isCurrent) problems.push(`the ${level} tool list changed but a session opened before is kept`);
29
+ if (!hasChanged && !isCurrent) problems.push(`the ${level} tool list did not change but a session opened before is renewed`);
30
+ }
31
+ if (unchanged.length === LEVELS.length) problems.push(`the tool list did not change for any access level (${unchanged.join(", ")})`);
32
+ if (problems.length > 0) throw new Error(`@softure-ai/mcp-access: sessions do not follow the tool list: ${problems.join("; ")}`);
33
+ }
34
+
35
+ async function readVersion(factory: McpServerFactory, identity: McpServerIdentity): Promise<string> {
36
+ const reader = (factory as Partial<VersionedMcpServerFactory>).toolListVersion;
37
+ if (typeof reader !== "function") {
38
+ throw new Error("@softure-ai/mcp-access: the factory has no toolListVersion; build it with createMcpServerFactory or pass toolListVersion to createMcpEndpoint");
39
+ }
40
+ return reader(identity);
41
+ }