@brightbeamai/chap-coordinator-mcp 0.2.9

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.
@@ -0,0 +1,171 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { z } from 'zod';
3
+ import { Coordinator, Envelope } from '@brightbeamai/chap-coordinator';
4
+ export { JsonSchema, SCHEMAS, TOOL_NAMES, coerceToolArgs, methodForTool, schemaFor } from './schemas.cjs';
5
+ export { TOOL_DESCRIPTIONS } from './tools.cjs';
6
+
7
+ /**
8
+ * Per-request protocol envelope (MCP 2026-07-28).
9
+ *
10
+ * The 2026 revision is stateless: instead of negotiating once through an
11
+ * `initialize` handshake, every request declares its protocol version and
12
+ * client capabilities in `_meta`, and the server accepts or rejects each
13
+ * request on its own. This module implements that classification so the
14
+ * TypeScript adapter behaves the same way the Python `mcp` 2.x SDK does,
15
+ * which enforces the same rules natively.
16
+ *
17
+ * Two version sets, deliberately different:
18
+ *
19
+ * - `MODERN_PROTOCOL_VERSIONS` is what may be declared per request. Only
20
+ * revisions that use per-request metadata qualify, so `2025-11-25` is
21
+ * not a legal per-request declaration: it is negotiated by handshake.
22
+ * This is the set named in an `UnsupportedProtocolVersionError`, so a
23
+ * client retrying from that list always retries with something usable.
24
+ *
25
+ * - `SUPPORTED_PROTOCOL_VERSIONS` is what `server/discover` advertises. It
26
+ * spans both eras because CHAP serves both: a modern client uses
27
+ * `2026-07-28` per request, a handshake-era client negotiates
28
+ * `2025-11-25` through `initialize`.
29
+ */
30
+ /**
31
+ * A JSON-RPC error carrying a spec-exact `message`.
32
+ *
33
+ * The SDK's own `McpError` rewrites the message to `MCP error <code>: <text>`
34
+ * and the protocol layer puts that rewritten string on the wire, which would
35
+ * make CHAP answer `MCP error -32022: Unsupported protocol version` where the
36
+ * specification, and the Python implementation, both say `Unsupported
37
+ * protocol version`. The protocol layer reads `code`, `message` and `data`
38
+ * off whatever is thrown, so a plain `Error` carrying those fields
39
+ * serialises exactly as written.
40
+ */
41
+ declare class ProtocolError extends Error {
42
+ readonly code: number;
43
+ readonly data?: unknown;
44
+ constructor(code: number, message: string, data?: unknown);
45
+ }
46
+ /** `_meta` key carrying the protocol version of a single request. */
47
+ declare const META_PROTOCOL_VERSION = "io.modelcontextprotocol/protocolVersion";
48
+ /** `_meta` key carrying client identity. Optional per the specification. */
49
+ declare const META_CLIENT_INFO = "io.modelcontextprotocol/clientInfo";
50
+ /** `_meta` key carrying client capabilities. Required on a modern request. */
51
+ declare const META_CLIENT_CAPABILITIES = "io.modelcontextprotocol/clientCapabilities";
52
+ /**
53
+ * `UnsupportedProtocolVersionError`. The specification reserves -32020 to
54
+ * -32099 for its own codes; -32022 is the one it assigns to a version the
55
+ * server does not implement.
56
+ */
57
+ declare const UNSUPPORTED_PROTOCOL_VERSION = -32022;
58
+ /** Revisions that may be declared in a per-request `_meta` envelope. */
59
+ declare const MODERN_PROTOCOL_VERSIONS: readonly ["2026-07-28"];
60
+ /**
61
+ * Revisions advertised by `server/discover`, newest first. The tool surface
62
+ * is identical across both: CHAP exposes plain tools with JSON Schema
63
+ * inputs, which the 2026-07-28 stateless core carries unchanged.
64
+ */
65
+ declare const SUPPORTED_PROTOCOL_VERSIONS: readonly ["2026-07-28", "2025-11-25"];
66
+ /**
67
+ * Which era a single request belongs to. `modern` means it carried a valid
68
+ * per-request envelope; `legacy` means it carried no protocol version at
69
+ * all and therefore belongs to a handshake-negotiated session.
70
+ */
71
+ type ProtocolEra = "modern" | "legacy";
72
+ /**
73
+ * Classify one request by its `_meta` envelope, rejecting a malformed or
74
+ * unsupported one.
75
+ *
76
+ * The rejection ladder matches the Python SDK's, rung for rung, so the two
77
+ * implementations answer an identical probe identically:
78
+ *
79
+ * 1. no `_meta`, or no protocol version in it, is a handshake-era request
80
+ * 2. a declared version with no `clientCapabilities` is malformed: -32602
81
+ * 3. a non-string version is malformed: -32602
82
+ * 4. a version outside `MODERN_PROTOCOL_VERSIONS`: -32022
83
+ *
84
+ * @throws ProtocolError on a malformed or unsupported envelope.
85
+ */
86
+ declare function classifyEnvelope(params: unknown): ProtocolEra;
87
+
88
+ /**
89
+ * @brightbeamai/chap-coordinator-mcp
90
+ *
91
+ * MCP server adapter for a CHAP Coordinator. Wraps a Coordinator
92
+ * instance and exposes every CHAP method as an MCP tool.
93
+ *
94
+ * Spec target: MCP 2026-07-28 (current), serving MCP 2025-11-25
95
+ * clients as well. CHAP 0.2.
96
+ *
97
+ * Usage (stdio):
98
+ *
99
+ * import { Coordinator } from "@brightbeamai/chap-coordinator";
100
+ * import { makeChapMcpServer } from "@brightbeamai/chap-coordinator-mcp";
101
+ * import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
102
+ *
103
+ * const coord = new Coordinator({ ... });
104
+ * const server = makeChapMcpServer(coord, { name: "chap", version: "0.2.9" });
105
+ * await server.connect(new StdioServerTransport());
106
+ *
107
+ * Usage (Streamable HTTP): see reference/mcp-server-ts/server.ts.
108
+ *
109
+ * Architecture notes:
110
+ *
111
+ * - One Coordinator -> one MCP server. Multi-workspace is handled
112
+ * inside the Coordinator (workspaces are addressable by id).
113
+ *
114
+ * - The adapter holds no state. Every tool call translates to a
115
+ * JSON-RPC envelope and dispatches through coord.dispatch().
116
+ *
117
+ * - Tool naming follows "chap.<method>" so the prefix avoids
118
+ * collisions with other MCP servers a client might load.
119
+ *
120
+ * - Tool inputs are described by JSON Schemas (not Zod) and are
121
+ * passed through to the Coordinator without re-validating at the
122
+ * MCP layer. The Coordinator's own dispatch validates params and
123
+ * returns spec-correct JSON-RPC error codes, which we surface as
124
+ * MCP tool errors. This keeps the schema definitions single-sourced.
125
+ *
126
+ * - Authentication is intentionally out of scope at this layer.
127
+ * Apply OAuth 2.1 / Streamable HTTP auth at the transport layer
128
+ * per MCP's auth model.
129
+ */
130
+
131
+ /** `_meta` key carrying server identity (MCP 2026-07-28, SEP-2575). */
132
+ declare const META_SERVER_INFO = "io.modelcontextprotocol/serverInfo";
133
+ /**
134
+ * Request schema for `server/discover` (MCP 2026-07-28, SEP-2575).
135
+ *
136
+ * Hand-written because the pinned SDK major predates the 2026 era. The
137
+ * shape is permissive on `params` so a probe carrying `_meta`
138
+ * (protocolVersion, clientInfo, clientCapabilities) validates, and a
139
+ * bare probe with no params validates too.
140
+ */
141
+ declare const ServerDiscoverRequestSchema: z.ZodObject<{
142
+ method: z.ZodLiteral<"server/discover">;
143
+ params: z.ZodOptional<z.ZodObject<{
144
+ _meta: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
145
+ }, z.core.$loose>>;
146
+ }, z.core.$strip>;
147
+ interface ChapMcpOptions {
148
+ /** Server name advertised to MCP clients. Default: "chap". */
149
+ name?: string;
150
+ /** Server version. Default: "0.2.9". */
151
+ version?: string;
152
+ /** Override the list of CHAP methods to expose. Default: all 39. */
153
+ toolFilter?: (toolName: string) => boolean;
154
+ /** Optional id generator for the envelopes emitted by tool calls. */
155
+ envelopeIdFactory?: () => string | number;
156
+ }
157
+ /**
158
+ * Wrap a CHAP Coordinator as an MCP server. The returned ``Server``
159
+ * has ``tools/list`` and ``tools/call`` handlers registered for every
160
+ * CHAP method; pass it to a transport (stdio, Streamable HTTP) to
161
+ * start serving.
162
+ */
163
+ declare function makeChapMcpServer(coord: Coordinator, options?: ChapMcpOptions): Server;
164
+ /**
165
+ * Lower-level helper: translate a tool call to a CHAP envelope and
166
+ * back. Useful for tests and for embedding the adapter inside a
167
+ * larger MCP server that registers its own additional tools.
168
+ */
169
+ declare function dispatchToolCall(coord: Coordinator, toolName: string, args: Record<string, unknown>, envelopeId?: string | number): Envelope;
170
+
171
+ export { type ChapMcpOptions, META_CLIENT_CAPABILITIES, META_CLIENT_INFO, META_PROTOCOL_VERSION, META_SERVER_INFO, MODERN_PROTOCOL_VERSIONS, type ProtocolEra, ProtocolError, SUPPORTED_PROTOCOL_VERSIONS, ServerDiscoverRequestSchema, UNSUPPORTED_PROTOCOL_VERSION, classifyEnvelope, dispatchToolCall, makeChapMcpServer };
@@ -0,0 +1,171 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { z } from 'zod';
3
+ import { Coordinator, Envelope } from '@brightbeamai/chap-coordinator';
4
+ export { JsonSchema, SCHEMAS, TOOL_NAMES, coerceToolArgs, methodForTool, schemaFor } from './schemas.js';
5
+ export { TOOL_DESCRIPTIONS } from './tools.js';
6
+
7
+ /**
8
+ * Per-request protocol envelope (MCP 2026-07-28).
9
+ *
10
+ * The 2026 revision is stateless: instead of negotiating once through an
11
+ * `initialize` handshake, every request declares its protocol version and
12
+ * client capabilities in `_meta`, and the server accepts or rejects each
13
+ * request on its own. This module implements that classification so the
14
+ * TypeScript adapter behaves the same way the Python `mcp` 2.x SDK does,
15
+ * which enforces the same rules natively.
16
+ *
17
+ * Two version sets, deliberately different:
18
+ *
19
+ * - `MODERN_PROTOCOL_VERSIONS` is what may be declared per request. Only
20
+ * revisions that use per-request metadata qualify, so `2025-11-25` is
21
+ * not a legal per-request declaration: it is negotiated by handshake.
22
+ * This is the set named in an `UnsupportedProtocolVersionError`, so a
23
+ * client retrying from that list always retries with something usable.
24
+ *
25
+ * - `SUPPORTED_PROTOCOL_VERSIONS` is what `server/discover` advertises. It
26
+ * spans both eras because CHAP serves both: a modern client uses
27
+ * `2026-07-28` per request, a handshake-era client negotiates
28
+ * `2025-11-25` through `initialize`.
29
+ */
30
+ /**
31
+ * A JSON-RPC error carrying a spec-exact `message`.
32
+ *
33
+ * The SDK's own `McpError` rewrites the message to `MCP error <code>: <text>`
34
+ * and the protocol layer puts that rewritten string on the wire, which would
35
+ * make CHAP answer `MCP error -32022: Unsupported protocol version` where the
36
+ * specification, and the Python implementation, both say `Unsupported
37
+ * protocol version`. The protocol layer reads `code`, `message` and `data`
38
+ * off whatever is thrown, so a plain `Error` carrying those fields
39
+ * serialises exactly as written.
40
+ */
41
+ declare class ProtocolError extends Error {
42
+ readonly code: number;
43
+ readonly data?: unknown;
44
+ constructor(code: number, message: string, data?: unknown);
45
+ }
46
+ /** `_meta` key carrying the protocol version of a single request. */
47
+ declare const META_PROTOCOL_VERSION = "io.modelcontextprotocol/protocolVersion";
48
+ /** `_meta` key carrying client identity. Optional per the specification. */
49
+ declare const META_CLIENT_INFO = "io.modelcontextprotocol/clientInfo";
50
+ /** `_meta` key carrying client capabilities. Required on a modern request. */
51
+ declare const META_CLIENT_CAPABILITIES = "io.modelcontextprotocol/clientCapabilities";
52
+ /**
53
+ * `UnsupportedProtocolVersionError`. The specification reserves -32020 to
54
+ * -32099 for its own codes; -32022 is the one it assigns to a version the
55
+ * server does not implement.
56
+ */
57
+ declare const UNSUPPORTED_PROTOCOL_VERSION = -32022;
58
+ /** Revisions that may be declared in a per-request `_meta` envelope. */
59
+ declare const MODERN_PROTOCOL_VERSIONS: readonly ["2026-07-28"];
60
+ /**
61
+ * Revisions advertised by `server/discover`, newest first. The tool surface
62
+ * is identical across both: CHAP exposes plain tools with JSON Schema
63
+ * inputs, which the 2026-07-28 stateless core carries unchanged.
64
+ */
65
+ declare const SUPPORTED_PROTOCOL_VERSIONS: readonly ["2026-07-28", "2025-11-25"];
66
+ /**
67
+ * Which era a single request belongs to. `modern` means it carried a valid
68
+ * per-request envelope; `legacy` means it carried no protocol version at
69
+ * all and therefore belongs to a handshake-negotiated session.
70
+ */
71
+ type ProtocolEra = "modern" | "legacy";
72
+ /**
73
+ * Classify one request by its `_meta` envelope, rejecting a malformed or
74
+ * unsupported one.
75
+ *
76
+ * The rejection ladder matches the Python SDK's, rung for rung, so the two
77
+ * implementations answer an identical probe identically:
78
+ *
79
+ * 1. no `_meta`, or no protocol version in it, is a handshake-era request
80
+ * 2. a declared version with no `clientCapabilities` is malformed: -32602
81
+ * 3. a non-string version is malformed: -32602
82
+ * 4. a version outside `MODERN_PROTOCOL_VERSIONS`: -32022
83
+ *
84
+ * @throws ProtocolError on a malformed or unsupported envelope.
85
+ */
86
+ declare function classifyEnvelope(params: unknown): ProtocolEra;
87
+
88
+ /**
89
+ * @brightbeamai/chap-coordinator-mcp
90
+ *
91
+ * MCP server adapter for a CHAP Coordinator. Wraps a Coordinator
92
+ * instance and exposes every CHAP method as an MCP tool.
93
+ *
94
+ * Spec target: MCP 2026-07-28 (current), serving MCP 2025-11-25
95
+ * clients as well. CHAP 0.2.
96
+ *
97
+ * Usage (stdio):
98
+ *
99
+ * import { Coordinator } from "@brightbeamai/chap-coordinator";
100
+ * import { makeChapMcpServer } from "@brightbeamai/chap-coordinator-mcp";
101
+ * import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
102
+ *
103
+ * const coord = new Coordinator({ ... });
104
+ * const server = makeChapMcpServer(coord, { name: "chap", version: "0.2.9" });
105
+ * await server.connect(new StdioServerTransport());
106
+ *
107
+ * Usage (Streamable HTTP): see reference/mcp-server-ts/server.ts.
108
+ *
109
+ * Architecture notes:
110
+ *
111
+ * - One Coordinator -> one MCP server. Multi-workspace is handled
112
+ * inside the Coordinator (workspaces are addressable by id).
113
+ *
114
+ * - The adapter holds no state. Every tool call translates to a
115
+ * JSON-RPC envelope and dispatches through coord.dispatch().
116
+ *
117
+ * - Tool naming follows "chap.<method>" so the prefix avoids
118
+ * collisions with other MCP servers a client might load.
119
+ *
120
+ * - Tool inputs are described by JSON Schemas (not Zod) and are
121
+ * passed through to the Coordinator without re-validating at the
122
+ * MCP layer. The Coordinator's own dispatch validates params and
123
+ * returns spec-correct JSON-RPC error codes, which we surface as
124
+ * MCP tool errors. This keeps the schema definitions single-sourced.
125
+ *
126
+ * - Authentication is intentionally out of scope at this layer.
127
+ * Apply OAuth 2.1 / Streamable HTTP auth at the transport layer
128
+ * per MCP's auth model.
129
+ */
130
+
131
+ /** `_meta` key carrying server identity (MCP 2026-07-28, SEP-2575). */
132
+ declare const META_SERVER_INFO = "io.modelcontextprotocol/serverInfo";
133
+ /**
134
+ * Request schema for `server/discover` (MCP 2026-07-28, SEP-2575).
135
+ *
136
+ * Hand-written because the pinned SDK major predates the 2026 era. The
137
+ * shape is permissive on `params` so a probe carrying `_meta`
138
+ * (protocolVersion, clientInfo, clientCapabilities) validates, and a
139
+ * bare probe with no params validates too.
140
+ */
141
+ declare const ServerDiscoverRequestSchema: z.ZodObject<{
142
+ method: z.ZodLiteral<"server/discover">;
143
+ params: z.ZodOptional<z.ZodObject<{
144
+ _meta: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
145
+ }, z.core.$loose>>;
146
+ }, z.core.$strip>;
147
+ interface ChapMcpOptions {
148
+ /** Server name advertised to MCP clients. Default: "chap". */
149
+ name?: string;
150
+ /** Server version. Default: "0.2.9". */
151
+ version?: string;
152
+ /** Override the list of CHAP methods to expose. Default: all 39. */
153
+ toolFilter?: (toolName: string) => boolean;
154
+ /** Optional id generator for the envelopes emitted by tool calls. */
155
+ envelopeIdFactory?: () => string | number;
156
+ }
157
+ /**
158
+ * Wrap a CHAP Coordinator as an MCP server. The returned ``Server``
159
+ * has ``tools/list`` and ``tools/call`` handlers registered for every
160
+ * CHAP method; pass it to a transport (stdio, Streamable HTTP) to
161
+ * start serving.
162
+ */
163
+ declare function makeChapMcpServer(coord: Coordinator, options?: ChapMcpOptions): Server;
164
+ /**
165
+ * Lower-level helper: translate a tool call to a CHAP envelope and
166
+ * back. Useful for tests and for embedding the adapter inside a
167
+ * larger MCP server that registers its own additional tools.
168
+ */
169
+ declare function dispatchToolCall(coord: Coordinator, toolName: string, args: Record<string, unknown>, envelopeId?: string | number): Envelope;
170
+
171
+ export { type ChapMcpOptions, META_CLIENT_CAPABILITIES, META_CLIENT_INFO, META_PROTOCOL_VERSION, META_SERVER_INFO, MODERN_PROTOCOL_VERSIONS, type ProtocolEra, ProtocolError, SUPPORTED_PROTOCOL_VERSIONS, ServerDiscoverRequestSchema, UNSUPPORTED_PROTOCOL_VERSION, classifyEnvelope, dispatchToolCall, makeChapMcpServer };