@jangmanj/mcp 0.0.0-stage → 0.12.3

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 (184) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +337 -2
  3. package/adapters/pi/HOST-NOTES.md +65 -0
  4. package/adapters/pi/bridge.ts +721 -0
  5. package/adapters/pi/config.ts +364 -0
  6. package/adapters/pi/content-offloaded.ts +53 -0
  7. package/adapters/pi/content.ts +78 -0
  8. package/adapters/pi/diagnostics.ts +26 -0
  9. package/adapters/pi/elicitation.ts +133 -0
  10. package/adapters/pi/index.ts +210 -0
  11. package/adapters/pi/oauth.ts +219 -0
  12. package/adapters/pi/pi-api.ts +133 -0
  13. package/adapters/pi/resources.ts +177 -0
  14. package/adapters/pi/result-files.ts +32 -0
  15. package/adapters/pi/tasks.ts +49 -0
  16. package/dist/auth/client-credentials.d.ts +65 -0
  17. package/dist/auth/client-credentials.js +388 -0
  18. package/dist/auth/discovery.d.ts +28 -0
  19. package/dist/auth/discovery.js +125 -0
  20. package/dist/auth/index.d.ts +9 -0
  21. package/dist/auth/index.js +5 -0
  22. package/dist/auth/node-ports.d.ts +8 -0
  23. package/dist/auth/node-ports.js +67 -0
  24. package/dist/auth/protocol.d.ts +12 -0
  25. package/dist/auth/protocol.js +39 -0
  26. package/dist/auth/provider.d.ts +114 -0
  27. package/dist/auth/provider.js +407 -0
  28. package/dist/auth/safe-fetch.d.ts +14 -0
  29. package/dist/auth/safe-fetch.js +130 -0
  30. package/dist/client/events.d.ts +33 -0
  31. package/dist/client/events.js +84 -0
  32. package/dist/client/index.d.ts +65 -0
  33. package/dist/client/index.js +128 -0
  34. package/dist/core/auth-types.d.ts +36 -0
  35. package/dist/core/auth-types.js +11 -0
  36. package/dist/core/bearer-challenge.d.ts +3 -0
  37. package/dist/core/bearer-challenge.js +63 -0
  38. package/dist/core/cancel.d.ts +70 -0
  39. package/dist/core/cancel.js +177 -0
  40. package/dist/core/extension.d.ts +33 -0
  41. package/dist/core/extension.js +145 -0
  42. package/dist/core/index.d.ts +73 -0
  43. package/dist/core/index.js +129 -0
  44. package/dist/core/jsonrpc.d.ts +49 -0
  45. package/dist/core/jsonrpc.js +80 -0
  46. package/dist/core/lifecycle.d.ts +128 -0
  47. package/dist/core/lifecycle.js +251 -0
  48. package/dist/core/outcome.d.ts +98 -0
  49. package/dist/core/outcome.js +32 -0
  50. package/dist/core/ports.d.ts +144 -0
  51. package/dist/core/ports.js +67 -0
  52. package/dist/core/progress.d.ts +81 -0
  53. package/dist/core/progress.js +205 -0
  54. package/dist/core/request.d.ts +108 -0
  55. package/dist/core/request.js +578 -0
  56. package/dist/core/schema/index.d.ts +1 -0
  57. package/dist/core/schema/index.js +1 -0
  58. package/dist/core/schema/types.d.ts +4154 -0
  59. package/dist/core/schema/types.js +6 -0
  60. package/dist/core/telemetry.d.ts +25 -0
  61. package/dist/core/telemetry.js +37 -0
  62. package/dist/ext-events/index.d.ts +79 -0
  63. package/dist/ext-events/index.js +218 -0
  64. package/dist/ext-events/model.d.ts +10 -0
  65. package/dist/ext-events/model.js +6 -0
  66. package/dist/ext-events/push.d.ts +18 -0
  67. package/dist/ext-events/push.js +101 -0
  68. package/dist/ext-events/stream.d.ts +6 -0
  69. package/dist/ext-events/stream.js +60 -0
  70. package/dist/ext-server-cards/index.d.ts +107 -0
  71. package/dist/ext-server-cards/index.js +249 -0
  72. package/dist/ext-skills/cache.d.ts +20 -0
  73. package/dist/ext-skills/cache.js +101 -0
  74. package/dist/ext-skills/client.d.ts +82 -0
  75. package/dist/ext-skills/client.js +292 -0
  76. package/dist/ext-skills/extension.d.ts +16 -0
  77. package/dist/ext-skills/extension.js +29 -0
  78. package/dist/ext-skills/index.d.ts +4 -0
  79. package/dist/ext-skills/index.js +4 -0
  80. package/dist/ext-skills/verify.d.ts +57 -0
  81. package/dist/ext-skills/verify.js +168 -0
  82. package/dist/index.d.ts +1 -0
  83. package/dist/index.js +1 -0
  84. package/dist/mrtr-cache/cache.d.ts +51 -0
  85. package/dist/mrtr-cache/cache.js +295 -0
  86. package/dist/mrtr-cache/client.d.ts +24 -0
  87. package/dist/mrtr-cache/client.js +44 -0
  88. package/dist/mrtr-cache/common.d.ts +10 -0
  89. package/dist/mrtr-cache/common.js +9 -0
  90. package/dist/mrtr-cache/elicitation.d.ts +42 -0
  91. package/dist/mrtr-cache/elicitation.js +221 -0
  92. package/dist/mrtr-cache/index.d.ts +9 -0
  93. package/dist/mrtr-cache/index.js +8 -0
  94. package/dist/mrtr-cache/mrtr.d.ts +37 -0
  95. package/dist/mrtr-cache/mrtr.js +250 -0
  96. package/dist/mrtr-cache/refresh.d.ts +8 -0
  97. package/dist/mrtr-cache/refresh.js +30 -0
  98. package/dist/mrtr-cache/stream.d.ts +5 -0
  99. package/dist/mrtr-cache/stream.js +18 -0
  100. package/dist/mrtr-cache/subscriptions.d.ts +96 -0
  101. package/dist/mrtr-cache/subscriptions.js +288 -0
  102. package/dist/mrtr-cache/tasks.d.ts +103 -0
  103. package/dist/mrtr-cache/tasks.js +444 -0
  104. package/dist/ops/common.d.ts +18 -0
  105. package/dist/ops/common.js +39 -0
  106. package/dist/ops/completion.d.ts +44 -0
  107. package/dist/ops/completion.js +65 -0
  108. package/dist/ops/content.d.ts +40 -0
  109. package/dist/ops/content.js +117 -0
  110. package/dist/ops/icons.d.ts +29 -0
  111. package/dist/ops/icons.js +82 -0
  112. package/dist/ops/index.d.ts +30 -0
  113. package/dist/ops/index.js +28 -0
  114. package/dist/ops/pagination.d.ts +32 -0
  115. package/dist/ops/pagination.js +65 -0
  116. package/dist/ops/prompts.d.ts +32 -0
  117. package/dist/ops/prompts.js +68 -0
  118. package/dist/ops/resources.d.ts +42 -0
  119. package/dist/ops/resources.js +104 -0
  120. package/dist/ops/schema.d.ts +39 -0
  121. package/dist/ops/schema.js +163 -0
  122. package/dist/ops/tools.d.ts +88 -0
  123. package/dist/ops/tools.js +169 -0
  124. package/dist/ops/uri-template.d.ts +3 -0
  125. package/dist/ops/uri-template.js +101 -0
  126. package/dist/server/index.d.ts +5 -0
  127. package/dist/server/index.js +5 -0
  128. package/dist/server/mrtr.d.ts +38 -0
  129. package/dist/server/mrtr.js +99 -0
  130. package/dist/server/protocol.d.ts +21 -0
  131. package/dist/server/protocol.js +49 -0
  132. package/dist/server/server.d.ts +105 -0
  133. package/dist/server/server.js +383 -0
  134. package/dist/server/skills.d.ts +45 -0
  135. package/dist/server/skills.js +73 -0
  136. package/dist/server/uri.d.ts +2 -0
  137. package/dist/server/uri.js +27 -0
  138. package/dist/server-events/bus.d.ts +40 -0
  139. package/dist/server-events/bus.js +84 -0
  140. package/dist/server-events/events.d.ts +36 -0
  141. package/dist/server-events/events.js +108 -0
  142. package/dist/server-events/index.d.ts +6 -0
  143. package/dist/server-events/index.js +6 -0
  144. package/dist/server-events/notices.d.ts +24 -0
  145. package/dist/server-events/notices.js +31 -0
  146. package/dist/server-events/subscriptions.d.ts +33 -0
  147. package/dist/server-events/subscriptions.js +77 -0
  148. package/dist/server-events/tasks.d.ts +43 -0
  149. package/dist/server-events/tasks.js +101 -0
  150. package/dist/server-events/types.d.ts +25 -0
  151. package/dist/server-events/types.js +34 -0
  152. package/dist/server-transport/auth.d.ts +20 -0
  153. package/dist/server-transport/auth.js +29 -0
  154. package/dist/server-transport/channel.d.ts +17 -0
  155. package/dist/server-transport/channel.js +47 -0
  156. package/dist/server-transport/http.d.ts +19 -0
  157. package/dist/server-transport/http.js +209 -0
  158. package/dist/server-transport/index.d.ts +4 -0
  159. package/dist/server-transport/index.js +4 -0
  160. package/dist/server-transport/stdio.d.ts +16 -0
  161. package/dist/server-transport/stdio.js +83 -0
  162. package/dist/telemetry-otel/index.d.ts +64 -0
  163. package/dist/telemetry-otel/index.js +196 -0
  164. package/dist/testing/index.d.ts +36 -0
  165. package/dist/testing/index.js +140 -0
  166. package/dist/transport-http/headers.d.ts +24 -0
  167. package/dist/transport-http/headers.js +140 -0
  168. package/dist/transport-http/index.d.ts +3 -0
  169. package/dist/transport-http/index.js +3 -0
  170. package/dist/transport-http/sse.d.ts +33 -0
  171. package/dist/transport-http/sse.js +152 -0
  172. package/dist/transport-http/transport.d.ts +66 -0
  173. package/dist/transport-http/transport.js +854 -0
  174. package/dist/transport-stdio/framing.d.ts +50 -0
  175. package/dist/transport-stdio/framing.js +154 -0
  176. package/dist/transport-stdio/index.d.ts +4 -0
  177. package/dist/transport-stdio/index.js +4 -0
  178. package/dist/transport-stdio/stderr-ring.d.ts +27 -0
  179. package/dist/transport-stdio/stderr-ring.js +105 -0
  180. package/dist/transport-stdio/transport.d.ts +142 -0
  181. package/dist/transport-stdio/transport.js +743 -0
  182. package/dist/transport-stdio/write-queue.d.ts +48 -0
  183. package/dist/transport-stdio/write-queue.js +147 -0
  184. package/package.json +134 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 JangMan-J
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,338 @@
1
- # Temporary Holding Version
1
+ # @jangmanj/mcp
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ MCP 2026-07-28 client library: core protocol/lifecycle, stdio and HTTP transports, OAuth/client-credentials auth, ops (tools/prompts/resources/completion), MRTR/cache/tasks, OpenTelemetry, and extensions.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @jangmanj/mcp
9
+ ```
10
+
11
+ ## Quickstart
12
+
13
+ Push first: subscribe to events and react as they arrive. Pull (tools, resources) is
14
+ there when you need it.
15
+
16
+ ```ts
17
+ import { connect } from "@jangmanj/mcp/client";
18
+
19
+ const connection = await connect({
20
+ stdio: { command: "npx", args: ["-y", "your-mcp-server"] },
21
+ reconnectOnDrop: true,
22
+ });
23
+
24
+ try {
25
+ // Events extension: live occurrences over events/stream (heartbeat + reconnect).
26
+ const mail = await connection.events.subscribe("mail", { folder: "inbox" });
27
+ for await (const event of mail) {
28
+ console.log(event.eventId, event.data);
29
+ }
30
+
31
+ // Standard MCP subscriptions: tools/prompts/resources list_changed, resource updates.
32
+ const changes = connection.listen({ tools: true });
33
+ await changes.acknowledged;
34
+ for await (const change of changes.events) {
35
+ console.log(change.method); // e.g. notifications/tools/list_changed -> re-list tools
36
+ }
37
+ } finally {
38
+ await connection.close();
39
+ }
40
+ ```
41
+
42
+ `connection.events` uses the optional `io.modelcontextprotocol/events` extension and
43
+ calls `server/discover` lazily to check support; `subscription.poll()` is available
44
+ when you explicitly want pull. `connection.listen()` is the spec-standard
45
+ `subscriptions/listen` stream.
46
+
47
+ Tools work the same connection, without `Outcome` matching:
48
+
49
+ ```ts
50
+ const tools = await connection.tools.list();
51
+ const result = await connection.tools.call("list_allowed_directories");
52
+ console.log(result.content);
53
+ ```
54
+
55
+ `tools.list()` / `tools.call()` return values or throw `McpOutcomeError`. For the full
56
+ protocol surface (MRTR, tasks, uncertain writes), use `connection.ops` directly and
57
+ match on `Outcome.kind`.
58
+
59
+ HTTP servers use `http:` instead of `stdio:`:
60
+
61
+ ```ts
62
+ import { ConfiguredBearer } from "@jangmanj/mcp/auth";
63
+
64
+ const connection = await connect({
65
+ http: {
66
+ url: "https://mcp.example.com/mcp",
67
+ authProvider: new ConfiguredBearer({
68
+ resource: "https://mcp.example.com/mcp",
69
+ token: process.env.MCP_TOKEN!,
70
+ }),
71
+ },
72
+ });
73
+ ```
74
+
75
+ For interactive OAuth (discovery, PKCE, CIMD/DCR), construct `OAuthAuthorization` from
76
+ `/auth` with your consent/browser/redirect ports and pass it as `authProvider`.
77
+
78
+ ## Building a server
79
+
80
+ `@jangmanj/mcp/server` covers the ordinary MCP 2026-07-28 request
81
+ surface; `@jangmanj/mcp/server-events` covers push. Both are
82
+ transport-agnostic — your host owns JSON-RPC dispatch, error mapping, and
83
+ listeners.
84
+
85
+ ```ts
86
+ import { McpServer } from "@jangmanj/mcp/server";
87
+ import { EventBus, EventsServer, SubscriptionsServer, TaskCenter } from "@jangmanj/mcp/server-events";
88
+
89
+ const bus = new EventBus();
90
+ const subscriptions = new SubscriptionsServer({ capabilities: { tools: { listChanged: true } } });
91
+ const server = new McpServer({
92
+ serverInfo: { name: "roger-wilco", version: "0.1.0" },
93
+ subscriptions,
94
+ events: new EventsServer({ bus }),
95
+ tasks: new TaskCenter({ bus }),
96
+ });
97
+
98
+ server.registerTool({
99
+ name: "roger_send",
100
+ inputSchema: { type: "object", properties: { body: { type: "string" } }, required: ["body"] },
101
+ handler: async ({ body }) => {
102
+ const mail = bus.publish("roger.mail", { body });
103
+ return { content: [{ type: "text", text: mail.eventId }] };
104
+ },
105
+ });
106
+ server.registerResource({ uri: "file:///board", name: "board", handler: () => "..." });
107
+ server.registerPrompt({ name: "reply", handler: () => ({ messages: [] }) });
108
+
109
+ // Ordinary JSON-RPC requests (tools/list, tools/call, resources/read,
110
+ // prompts/get, completion/complete, server/discover, events/*, tasks/*):
111
+ const result = await server.handleRequest(msg); // throws ServerError(code) for the host to map
112
+
113
+ // Long-lived streams still use host channels:
114
+ // subscriptions.listen(channel, params) / server.events?.stream(channel, params)
115
+ ```
116
+
117
+ Included: deterministic pagination with opaque cursors, required `resultType`
118
+ and cache hints, input/output schema validation (Ajv 2020-12), resource
119
+ templates, prompt arguments, completion capping, MRTR `input_required` and
120
+ HMAC-protected `requestState` helpers with single-use retry correlation,
121
+ task-handle results with TTL expiry, and skills publishing with verified
122
+ manifests. Deprecated Logging/Roots/Sampling are intentionally absent.
123
+
124
+ ## Server transports
125
+
126
+ `@jangmanj/mcp/server-transport` hosts an `McpServer` over real stdio or
127
+ streamable HTTP, including SSE push channels, `notifications/cancelled`, and
128
+ resource-side bearer auth:
129
+
130
+ ```ts
131
+ import { StdioServerHost, HttpServerHost, StaticBearerAuth, buildProtectedResourceMetadata } from "@jangmanj/mcp/server-transport";
132
+
133
+ // stdio: newline-delimited JSON-RPC on stdin/stdout
134
+ await new StdioServerHost({ server }).listen();
135
+
136
+ // HTTP: POST-only /mcp, header enforcement, SSE streams, optional bearer auth
137
+ const host = new HttpServerHost({ server, auth: new StaticBearerAuth(process.env.MCP_TOKEN!) });
138
+ host.createServer().listen(7477, "127.0.0.1");
139
+ ```
140
+
141
+ The HTTP host validates `MCP-Protocol-Version`, `Mcp-Method`, `Mcp-Name`, and
142
+ `Mcp-Param-*` against the request body, returns `-32020` on mismatch, enforces
143
+ Origin/Accept rules, and serves `/healthz`. `buildProtectedResourceMetadata()`
144
+ helps you publish RFC 9728 metadata for OAuth clients.
145
+
146
+ ## Push on the server side
147
+
148
+ `@jangmanj/mcp/server-events` is the modular, transport-agnostic server
149
+ companion: event logs with cursors, live `events/stream`, standard
150
+ `subscriptions/listen`, notices, and optional async tasks. It contains no
151
+ tools/resources/prompts registry of its own — that lives in
152
+ `@jangmanj/mcp/server` — so you can adopt push without adopting a
153
+ framework.
154
+
155
+ ```ts
156
+ import { EventBus, EventsServer, SubscriptionsServer, TaskCenter, Notices } from "@jangmanj/mcp/server-events";
157
+
158
+ const bus = new EventBus();
159
+ const events = new EventsServer({ bus });
160
+ const subscriptions = new SubscriptionsServer({ capabilities: { tools: { listChanged: true } } });
161
+ const tasks = new TaskCenter({ bus });
162
+ const notices = new Notices(bus);
163
+
164
+ // Ordinary requests return plain results for your host to send:
165
+ const page = events.poll({ name: "mail", arguments: {}, cursor: null });
166
+
167
+ // Long-lived requests use a host channel (notify/finish/onClose):
168
+ // events.stream(channel, { name: "mail", arguments: {}, cursor: null });
169
+ // subscriptions.listen(channel, { notifications: { toolsListChanged: true } });
170
+
171
+ // Application code publishes; subscribed clients receive push notices:
172
+ bus.publish("mail", { subject: "hello" });
173
+ subscriptions.notifyToolsChanged();
174
+ notices.info("ready");
175
+ const task = tasks.create({ pollIntervalMs: 1000 });
176
+ tasks.complete(task.taskId, { ok: true });
177
+ ```
178
+
179
+ Scope: `events/list`, `events/poll`, `events/stream`, `subscriptions/listen`,
180
+ `notifications/*` fanout, and `tasks/get`/`tasks/cancel`/`tasks/update` state.
181
+ Webhook delivery is intentionally out of scope; deprecated MCP Logging is not
182
+ used — notices travel as ordinary events. Skills publishing lives in
183
+ `@jangmanj/mcp/server` (`registerSkill`): manifests with computed
184
+ SHA-256/size, `skills/list`/`skills/get`, and file bytes via `resources/read`.
185
+
186
+ For discovery metadata, `@jangmanj/mcp/ext-server-cards` also includes a
187
+ card builder for servers you operate:
188
+
189
+ ```ts
190
+ import { buildServerCard, buildAiCatalog } from "@jangmanj/mcp/ext-server-cards";
191
+
192
+ const card = buildServerCard({
193
+ name: "roger-wilco",
194
+ version: "0.1.0",
195
+ description: "Durable cross-machine mail with push/poll event streams.",
196
+ remotes: [{ type: "streamable-http", url: "https://roger.example.net/mcp", supportedVersions: ["2026-07-28"] }],
197
+ });
198
+ const catalog = buildAiCatalog([card]); // serve as /.well-known/ai-catalog.json
199
+ ```
200
+
201
+ Cards are advisory only: primitive capability fields are rejected, and live
202
+ `server/discover` remains authoritative.
203
+
204
+ ## Errors, cancellation, progress
205
+
206
+ - **Simple mode** (`/client`): failures throw `McpOutcomeError`; inspect `error.outcome.kind`
207
+ for `jsonrpc-error`, `transport-failure`, `tool-isError`, etc.
208
+ - **Full mode** (`/ops` + `/core`): every call returns a discriminated `Outcome` instead of
209
+ throwing. `unwrapOutcome(outcome)` converts to throw-style.
210
+ - Pass `signal` (AbortSignal) and `timeoutMs` in request options to cancel or bound a call;
211
+ pass `onProgress` to receive progress notifications. Cancellation is cooperative and
212
+ sends `notifications/cancelled` when the request is already on the wire.
213
+ - `listAll()` follows cursors for you; single-page `list({ cursor })` is available when you
214
+ need manual pagination. Cursors are opaque strings — never parse them.
215
+
216
+ ## Testing your own tools
217
+
218
+ `@jangmanj/mcp/testing` provides an in-memory transport and a scriptable tool
219
+ server — no child processes or sockets:
220
+
221
+ ```ts
222
+ import { connect } from "@jangmanj/mcp/client";
223
+ import { createToolServer } from "@jangmanj/mcp/testing";
224
+
225
+ const connection = await connect({
226
+ transport: createToolServer({
227
+ echo: { handler: (args) => ({ content: [{ type: "text", text: String(args.message) }] }) },
228
+ }),
229
+ });
230
+ ```
231
+
232
+ Throw `FakeJsonRpcError(code, message)` from a handler to simulate protocol errors.
233
+ `transport.push(notification, requestId?)` injects notifications into active request
234
+ streams, so you can test pushed events and `list_changed` without a live server.
235
+
236
+ ## Low-level API
237
+
238
+ The quickstart above is a thin wrapper. The underlying pieces remain available when you
239
+ need them:
240
+
241
+ ```ts
242
+ import { McpClient } from "@jangmanj/mcp";
243
+ import { createOps } from "@jangmanj/mcp/ops";
244
+ import { StdioTransport } from "@jangmanj/mcp/transport-stdio";
245
+
246
+ const client = new McpClient({
247
+ transport: new StdioTransport({ command: "npx", args: ["-y", "@modelcontextprotocol/server-filesystem", "."] }),
248
+ server: { key: "filesystem" },
249
+ });
250
+ const ops = createOps(client);
251
+ // ... await ops.tools.listAll() / ops.tools.call(...), then await client.close()
252
+ ```
253
+
254
+ ## Compatibility expectations
255
+
256
+ - This library targets **MCP 2026-07-28** (stateless, per-request `_meta`). Older servers
257
+ are detected and diagnosed via era probing; it is not a full 2025-style `initialize` client.
258
+ - Node **>= 22.19**, ESM-first (CJS `require(esm)` works on supported Node versions).
259
+ - Roots, sampling, and logging are deprecated upstream and intentionally not implemented.
260
+
261
+ ## Subpath exports
262
+
263
+ - `@jangmanj/mcp` (re-exports `./core`)
264
+ - `@jangmanj/mcp/core`
265
+ - `@jangmanj/mcp/client` (high-level `connect()` API)
266
+ - `@jangmanj/mcp/testing` (in-memory transport / fake tool server)
267
+ - `@jangmanj/mcp/server-events` (push-first server building blocks)
268
+ - `@jangmanj/mcp/server` (tools/resources/prompts/discover/completion server)
269
+ - `@jangmanj/mcp/server-transport` (stdio + streamable HTTP server hosts)
270
+ - `@jangmanj/mcp/auth`
271
+ - `@jangmanj/mcp/ops`
272
+ - `@jangmanj/mcp/mrtr-cache`
273
+ - `@jangmanj/mcp/telemetry-otel`
274
+ - `@jangmanj/mcp/transport-http`
275
+ - `@jangmanj/mcp/transport-stdio`
276
+ - `@jangmanj/mcp/ext-events`
277
+ - `@jangmanj/mcp/ext-server-cards`
278
+ - `@jangmanj/mcp/ext-skills`
279
+
280
+ ## TypeScript
281
+
282
+ - Use `"moduleResolution": "NodeNext"` (or `"Bundler"`) in your consumer `tsconfig.json` so subpath exports resolve cleanly.
283
+ - If your consumer `tsconfig.json` overrides `"lib"` without `"DOM"` (for example `"lib": ["ES2023"]`), install `@types/node` and include `"types": ["node"]` so standard Web/Node globals (`AbortSignal`, `URL`, `Response`, `CryptoKey`) are typed.
284
+
285
+ ## Pi extension
286
+
287
+ This package doubles as a [Pi](https://pi.dev) package: installing it registers an
288
+ extension that replaces Pi's built-in MCP session support with a bridge built on
289
+ this library (transports, lifecycle, ops, MRTR) and exposes each server's tools to
290
+ Pi as `mcp__<server>__<tool>`.
291
+
292
+ ```sh
293
+ pi install npm:@jangmanj/mcp
294
+ # or from a checkout / git ref:
295
+ pi install ./path/to/mcp-client
296
+ pi install git:github.com/JangMan-J/mcp-client@v0.1.1
297
+ ```
298
+
299
+ The extension reads the same `mcp.json` files as built-in MCP
300
+ (`$PI_CODING_AGENT_DIR/mcp.json` or `~/.pi/agent/mcp.json`, plus `.pi/mcp.json` in
301
+ trusted projects) with the same merge, interpolation (`${VAR}`, `!command`), and
302
+ exposure rules (`codemode` default, `direct`/`deferred`/`hidden`, `toolExposure`
303
+ patterns). `/mcp` lists server states; `/mcp reconnect <name>` restarts one.
304
+ Servers registered by other extensions via `pi.registerMcpServer()` are connected
305
+ too, with file configuration taking precedence.
306
+
307
+ The extension lives in `adapters/pi/` (isolated from the protocol packages).
308
+ Beyond tools, it covers built-in MCP parity: `list_mcp_resources`,
309
+ `list_mcp_resource_templates`, `read_mcp_resource`, dynamic `list_changed`
310
+ tool updates, `/mcp prompts|prompt|resources|task|enable|disable|exposure`
311
+ subcommands, MRTR elicitation through Pi's UI, task handles with `tasks/get`
312
+ and `tasks/cancel`, 20 KB middle-out text truncation with temp-file offload,
313
+ and a rotating `$PI_CODING_AGENT_DIR/mcp-client.log`.
314
+
315
+ OAuth works out of the box for HTTP servers without a static `Authorization` header: the
316
+ first 401 marks the server "needs sign-in" in `/mcp`, and `/mcp login <name>` runs the
317
+ authorization-code + PKCE flow with CIMD/DCR registration through the system browser.
318
+ Credentials persist in `$PI_CODING_AGENT_DIR/mcp-client-oauth.json` (issuer-keyed);
319
+ `/mcp logout <name>` deletes them. The full `oauth` config shape is supported:
320
+ `clientId`, `clientSecret`, `clientName`, `callbackPort`, `callbackUrl` (loopback HTTP),
321
+ `scope` (space-separated, merged with advertised scopes), and `authServerMetadataUrl`
322
+ (trusted as configured, skips PRM discovery).
323
+
324
+ A running Pi session picks the extension up on `/reload`; fresh starts load it
325
+ automatically.
326
+
327
+ ## Development
328
+
329
+ ```sh
330
+ npm run check
331
+ npm run smoke:pack
332
+ npm run gen:spec-matrix # regenerate SPEC.md (requirement ID -> source -> tests)
333
+ npm run check:spec-drift # compare the vendored schema + spec prose against upstream
334
+ ```
335
+
336
+ SPEC.md is the generated traceability matrix; CI fails when it is stale or a source
337
+ requirement ID is cited by no test and not exempted. `spec-drift.yml` (daily cron)
338
+ opens an issue when the upstream MCP 2026-07-28 schema or spec prose moves.
@@ -0,0 +1,65 @@
1
+ # Host touchpoints: pi-coding-agent vs other Pi modules
2
+
3
+ This adapter is written against a structural `ExtensionAPI` (`pi-api.ts`), not
4
+ imported Pi packages (the repo's `check-forbidden` rule forbids them). The
5
+ following touchpoints are **pi-coding-agent-specific** and cannot be moved to
6
+ `pi-ai`, `pi-codemode`, or `pi-durable` without a host adapter:
7
+
8
+ | Touchpoint | Where | Why it is host-specific |
9
+ |---|---|---|
10
+ | `pi.registerTool` / `pi.registerCommand` | `bridge.ts`, `index.ts` | Tool/command ownership and `/mcp` replacement semantics belong to pi-coding-agent's extension runner. |
11
+ | `exposure`, `namespace`, `annotations` | `bridge.ts` | These are pi-coding-agent's codemode/tool_search exposure model, not MCP concepts. |
12
+ | `pi.getActiveTools` / `setActiveTools` / `getAllTools` | `bridge.ts` (`ensureReachTools`) | Activating `codemode` / `tool_search` is a pi-coding-agent tool-loadout operation. |
13
+ | `before_agent_start` + `systemPromptOptions.sections` | `index.ts` | pi-coding-agent's system-prompt section model (`mcp_servers`). |
14
+ | `tool_call` waits for late registration | `index.ts` | pi-coding-agent's tool pipeline and `reachesTools` behavior. |
15
+ | `session_start` / `session_shutdown` / `mcp_servers_change` | `index.ts` | pi-coding-agent session lifecycle and `pi.registerMcpServer` registry. |
16
+ | `ctx.ui.notify/confirm/input/select` | `index.ts`, `oauth.ts`, `elicitation.ts` | pi-coding-agent TUI prompts. |
17
+ | `pi.getSettings().autoEnableCodemode` | `bridge.ts` | pi-coding-agent settings shape. |
18
+
19
+ Portable as-is:
20
+
21
+ - `PiContent` text/image blocks are structurally identical to `pi-ai`'s
22
+ `TextContent` / `ImageContent`.
23
+ - `config.ts` (mcp.json discovery/merge/interpolation) is host-neutral except
24
+ for the `PI_CODING_AGENT_DIR` convention, which other hosts may mirror.
25
+ - `bridge.ts`'s connection, OAuth, MRTR, resource, and result-conversion logic
26
+ only needs the small `ExtensionAPI` surface above; a durable host can wrap it
27
+ with its own `defineExtension`/`defineTool` adapter.
28
+
29
+ `pi-durable` (cellar) uses `defineExtension`/`defineTool` with a different
30
+ `execute(args, api, context)` signature and its own event delivery, so there is
31
+ no drop-in move into durable; it needs the thin wrapper described above.
32
+ Cellar also provides its own command registerer, so the `/mcp` command surface
33
+ (`index.ts`) is portable there directly; the remaining durable-specific work is
34
+ tool execution signatures, UI prompts, and session/event delivery.
35
+
36
+ ## Cellar equivalents (graph: home-jangmanj-Projects-cellar, 2026-11-25)
37
+
38
+ Mapped against `src/daemon/pi-extensions.ts` (indexed, no recorded coverage
39
+ issue). Most of this adapter's pi-coding-agent surface has a direct durable
40
+ equivalent:
41
+
42
+ | Adapter touchpoint | Cellar equivalent |
43
+ |---|---|
44
+ | `pi.registerTool` | `PiExtensionApi.registerTool` / `PiTool` (same execute shape) |
45
+ | `pi.registerCommand` | `pi-extensions.registerCommand` (UI doc commands) |
46
+ | `exposure` | `PiTool.exposure` (string) |
47
+ | `annotations` | `PiTool.annotations` |
48
+ | `namespace` | **No equivalent** — `PiTool` has no namespace field; codemode grouping must be rethought or added. |
49
+ | `getActiveTools` / `setActiveTools` / `getAllTools` | `pi-extensions.getActiveTools` / `setActiveTools` / `getAllTools` |
50
+ | `before_agent_start` | `pi-extensions.beforeAgentStart` (systemPrompt override supported) |
51
+ | `systemPromptOptions.sections` | **No equivalent** — `systemPromptOptions: {}` is passed empty; use `systemPrompt` override instead of an `mcp_servers` section. |
52
+ | `tool_call` | `pi-extensions.beforeTool` (mutable input, block, gate prompts) |
53
+ | `session_start` / `session_shutdown` | `pi-extensions.start` / `close` |
54
+ | `mcp_servers_change` | **No event found** — `registerMcpServer` / `unregisterMcpServer` / `getMcpServers` exist, but no change event; re-read on demand. |
55
+ | `ctx.ui.notify/confirm/input/select` | `pi-extensions.notify` / `confirm` / `input` / `select` |
56
+ | `ctx.cwd` / `hasUI` / `isProjectTrusted` | `ExtensionContext.cwd` / `hasUI` / `isProjectTrusted` |
57
+ | `pi.getSettings()` | `pi-extensions.getSettings` (merged settings; no `autoEnableCodemode` reference found) |
58
+ | `pi.registerMcpServer` | `PiExtensionApi.registerMcpServer` |
59
+
60
+ Net: a durable adapter can reuse nearly all of `bridge.ts` unchanged. The
61
+ adapter-specific deltas are (1) no `namespace`, (2) system-prompt override
62
+ instead of sections, (3) no `mcp_servers_change` event, and (4) no
63
+ `autoEnableCodemode` setting lookup. `mcp-host.ts` / `mcp-events.ts` were not
64
+ present in this moderate index generation; earlier full-index evidence had
65
+ `startMcpHost` called by `src.cli.mcp` and `src.daemon.main.globalMcp` / `mcp`.