pi-mcp-client 0.0.0 โ†’ 0.1.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 (4) hide show
  1. package/README.md +308 -0
  2. package/dist/index.js +3446 -0
  3. package/package.json +54 -8
  4. package/index.js +0 -2
package/README.md ADDED
@@ -0,0 +1,308 @@
1
+ # ๐Ÿ”Œ Pi MCP Client
2
+
3
+ MCP tools for Pi, discovered on demand and called natively through the official
4
+ TypeScript SDK. No bridge process and no invocation proxy.
5
+
6
+ ## ๐Ÿš€ Installation
7
+
8
+ ```sh
9
+ pi install npm:pi-mcp-client
10
+ ```
11
+
12
+ ## โœจ Usage
13
+
14
+ First, add a server to `~/.pi/agent/mcp.json`. This public documentation server
15
+ does not require credentials:
16
+
17
+ ```json
18
+ {
19
+ "mcpServers": {
20
+ "cloudflare-docs": {
21
+ "type": "http",
22
+ "url": "https://docs.mcp.cloudflare.com/mcp"
23
+ }
24
+ }
25
+ }
26
+ ```
27
+
28
+ Start a new Pi session and ask it to search Cloudflare's documentation. Use `/mcp`
29
+ to inspect the connection. For authenticated services, see [OAuth](#oauth) or
30
+ [secret commands](#secret-commands).
31
+
32
+ Pi searches for the tools it needs, then calls those tools directly. Search
33
+ loads up to five matching tools by default, or up to 50 with `limit`. Results
34
+ use local BM25-based ranking, with tool names weighted more strongly than
35
+ descriptions and support for prefix matching. Full schemas become available on
36
+ the next model turn, without a separate describe step. Previously loaded tools
37
+ remain available as the conversation continues.
38
+
39
+ ### Session behavior
40
+
41
+ - Tools accumulate rather than rotating with each prompt.
42
+ - Resume and branch navigation restore tools acquired on the selected branch.
43
+ - Compaction retains the acquired tool set. New sessions start fresh.
44
+ - Pi uses native deferred loading where supported by the model and provider.
45
+ Other providers receive the expanded tool list normally.
46
+ - Search respects server filters and Pi's tool exclusions. An explicit tool
47
+ allowlist must include both `mcp_search` and the native tools you want to load.
48
+
49
+ ### Commands
50
+
51
+ | Command | Purpose |
52
+ | --- | --- |
53
+ | `/mcp` | Show server connection status, catalog sizes, and the loaded tool count. |
54
+ | `/mcp auth <server>` | Authenticate an OAuth-enabled HTTP server. |
55
+ | `/mcp reconnect <server>` | Replace a connection and refresh its catalog. |
56
+ | `/mcp refresh <server>` | Refresh a server's catalog without loading additional tools. |
57
+
58
+ After refreshing a changed schema, search for the tool again to load its current
59
+ definition. Calls validate the live catalog before execution and refuse removed
60
+ or changed tools. The extension does not retry failed tool invocations; after an
61
+ interrupted call, check whether the operation completed before trying again.
62
+
63
+ ## โš™๏ธ Configuration
64
+
65
+ Add connections to `~/.pi/agent/mcp.json`, or `.mcp.json` in a trusted project.
66
+ These files use the common Claude/Cursor-style `mcpServers` format, not a universal
67
+ MCP configuration standard. VS Code's `servers` format and Codex's TOML format
68
+ are not supported.
69
+
70
+ `PI_CODING_AGENT_DIR` overrides the global Pi directory. Project connections
71
+ replace same-named global connections in full; connection fields are not merged.
72
+
73
+ ```json
74
+ {
75
+ "mcpServers": {
76
+ "docs": {
77
+ "type": "http",
78
+ "url": "https://mcp.example.com/mcp",
79
+ "headers": {
80
+ "Authorization": "Bearer ${DOCS_TOKEN}"
81
+ }
82
+ },
83
+ "local": {
84
+ "type": "stdio",
85
+ "command": "node",
86
+ "args": ["/absolute/path/to/server.js"],
87
+ "env": {
88
+ "DATABASE_URL": "${DATABASE_URL}"
89
+ }
90
+ }
91
+ }
92
+ }
93
+ ```
94
+
95
+ | Field | Purpose |
96
+ | --- | --- |
97
+ | `type` | Optional `stdio` or `http`. If omitted, inferred from `command` or `url`. A conflicting type is rejected. |
98
+ | `command`, `args` | Executable and arguments for a stdio server. No shell is used. |
99
+ | `cwd` | Working directory for stdio; defaults to Pi's current directory. Relative paths resolve there. |
100
+ | `env` | Additional environment variables for stdio. |
101
+ | `url` | Streamable HTTP endpoint; mutually exclusive with `command`. |
102
+ | `headers` | HTTP request headers, including optional bearer authentication. |
103
+
104
+ Strings in `command`, `args`, `cwd`, `env`, `url`, and `headers` support `${VAR}`
105
+ interpolation. Missing variables prevent that server from connecting.
106
+
107
+ Only stdio and Streamable HTTP are supported; `type: "sse"` is rejected rather
108
+ than treated as HTTP. Unsupported connection fields cause a configuration error
109
+ rather than silently changing their meaning.
110
+
111
+ ### Secret commands
112
+
113
+ In **`headers` and stdio `env` values only**, a leading `!` runs a secret-generating
114
+ shell command when the server connects:
115
+
116
+ ```json
117
+ {
118
+ "mcpServers": {
119
+ "example": {
120
+ "type": "http",
121
+ "url": "https://mcp.example.com/mcp",
122
+ "headers": {
123
+ "Authorization": "!token=$(op read 'op://Private/Example/token') && printf 'Bearer %s' \"$token\""
124
+ }
125
+ }
126
+ }
127
+ }
128
+ ```
129
+
130
+ These two fields also support Pi-style `$VAR` interpolation, `$$` for a literal
131
+ `$`, and `$!` for a literal `!`. Only a leading `!` in the original configuration
132
+ triggers execution; interpolated values and command output never do. Shell
133
+ commands handle their own variable expansion.
134
+
135
+ Commands use `/bin/sh` on Unix or Pi's shell selection on Windows, inherit Pi's
136
+ process environment, and run in the server's configured `cwd` (the project
137
+ directory by default). They run once per connection, including reconnections,
138
+ not during configuration loading, status display, or cached discovery. Cold
139
+ searches can connect and therefore execute commands. Concurrent connection
140
+ requests share the same resolution.
141
+
142
+ The client trims stdout and rejects empty output, nonzero exits, output above
143
+ 64 KiB, and resolution taking more than 10 seconds (or a shorter `timeoutMs`).
144
+ Session shutdown cancels pending commands. Cancelling an individual search stops
145
+ waiting but leaves shared connection work running for other callers. The client
146
+ discards command stderr and does not include resolved secrets in errors, session
147
+ records, or catalog caches. Commands themselves remain responsible for avoiding
148
+ side effects or writing secrets to disk. Only configure commands you trust;
149
+ project configuration still requires project trust.
150
+
151
+ ### Pi-specific options
152
+
153
+ Put descriptions, authentication choices, filters, and timeouts directly in each
154
+ `mcpServers.<server>` definition in `~/.pi/agent/mcp.json` (or a trusted project's
155
+ `.mcp.json`):
156
+
157
+ ```json
158
+ {
159
+ "mcpServers": {
160
+ "docs": {
161
+ "type": "http",
162
+ "url": "https://mcp.example.com/mcp",
163
+ "description": "Search product documentation",
164
+ "oauth": true,
165
+ "includeTools": ["get_*", "search_*"]
166
+ }
167
+ }
168
+ }
169
+ ```
170
+
171
+ | Field | Purpose |
172
+ | --- | --- |
173
+ | `description` | Short capability description for Pi's server directory. |
174
+ | `oauth` | Set to `true` to use OAuth instead of an Authorization header on an HTTP connection. |
175
+ | `disabled` | Prevent this server from connecting or exposing tools. |
176
+ | `includeTools` | Optional allowlist of original MCP tool names; `*` matches any sequence. An empty list exposes nothing. |
177
+ | `excludeTools` | Denylist applied after `includeTools`. |
178
+ | `timeoutMs` | Request timeout, from 100 to 600000 ms. Defaults: 15 seconds for discovery/HTTP requests, 30 seconds for stdio tool calls. |
179
+ | `protocol` | `auto` (default) for SDK protocol-version negotiation, or `legacy` for an explicit legacy handshake. |
180
+
181
+ A trusted project's server definition replaces the same-named global definition
182
+ in full, including these options. Fields and tool-filter lists are not merged.
183
+ Every definition must include a `url` or `command`, even when `disabled` is true.
184
+
185
+ These options are specific to Pi MCP Client, not standardized MCP connection
186
+ fields. Other clients may reject them when you copy a definition.
187
+
188
+ Configuration changes take effect when Pi reloads the extension or starts a new
189
+ session.
190
+
191
+ ### Discovery and caching
192
+
193
+ Connections start on demand, never while the extension factory loads. A search
194
+ without a cached catalog contacts configured servers, with at most four discoveries
195
+ in flight. A server-scoped search only contacts that server. Failed servers are
196
+ reported as unsearched, not mistaken for an empty catalog.
197
+
198
+ Catalogs are cached privately under `~/.pi/agent/cache/pi-mcp-client/`, keyed by
199
+ server configuration and working directory. Disk caches expire after 24 hours.
200
+ They contain tool metadata, not configured credentials. Cached search needs no
201
+ connection; invocation refreshes the live catalog before calling the tool.
202
+ Connections remain open until shutdown or explicit reconnection.
203
+
204
+ ### OAuth
205
+
206
+ Set `"oauth": true` under `mcpServers.<server>` in `mcp.json`, without an
207
+ Authorization header in its connection, then run `/mcp auth <server>`. Pi opens the
208
+ browser only for this explicit command. Automatic discovery never opens a browser.
209
+
210
+ OAuth tokens and client registrations are stored in the operating system
211
+ credential store, bound to the server URL and authorization-server issuer.
212
+ There is no plaintext credential fallback. PKCE verifiers and callback state stay
213
+ in memory.
214
+
215
+ The initial implementation supports dynamically registered public clients with a
216
+ local callback at `http://127.0.0.1:19847/callback`. The browser must be able to
217
+ reach that address on the Pi machine. Authentication times out after two minutes;
218
+ you can cancel it with Escape in the terminal UI.
219
+ Pre-registered OAuth clients, remote callback pasting, and headless interactive
220
+ OAuth are not supported yet. Use bearer headers for headless access.
221
+
222
+ ### Trust and permissions
223
+
224
+ Only load configuration you trust. Server executables and secret commands run
225
+ with your user permissions; trusted project configuration can replace global
226
+ connections and settings.
227
+
228
+ Server metadata is untrusted. Search activates tools but does not approve their
229
+ side effects or provide per-call confirmation. Use tool filters and Pi permission
230
+ extensions for additional controls. Cancelling a call does not guarantee that the
231
+ server rolled back its effects.
232
+
233
+ ## ๐Ÿงฐ Requirements
234
+
235
+ - Pi 0.85.1 or later, with additive dynamic tool loading.
236
+ - Node.js 22 or later.
237
+ - The server executable for stdio connections.
238
+ - An available OS credential store for OAuth. Linux requires a working Secret
239
+ Service/keyring session.
240
+
241
+ This extension uses `@modelcontextprotocol/client` 2.0.0 and defaults to automatic
242
+ SDK protocol-version negotiation. On stdio, negotiation probes using an additional
243
+ short-lived process. Set `"protocol": "legacy"` in a server definition if that
244
+ server requires an explicit legacy handshake.
245
+
246
+ ## ๐Ÿฉบ Troubleshooting
247
+
248
+ Start with `/mcp`. Failures use a consistent code, a short explanation, and a
249
+ recovery hint, for example:
250
+
251
+ ```text
252
+ linear: [authentication_required] Authentication is required. Run /mcp auth linear.
253
+ ```
254
+
255
+ Search and tool results also carry structured diagnostics in their result details:
256
+ `code`, `operation`, optional `server`, `message`, and `hint`. Partial discovery
257
+ keeps healthy servers' results and identifies servers it could not search. An
258
+ unavailable server is not an empty catalog.
259
+
260
+ | Code | What to check |
261
+ | --- | --- |
262
+ | `configuration_invalid` | JSON syntax, supported fields, transport type, and required environment variables. Reload Pi after editing. |
263
+ | `authentication_required` | Run `/mcp auth <server>` for OAuth, or check the Authorization header. |
264
+ | `permission_denied` | Account permissions, OAuth scopes, and service access policy. |
265
+ | `credential_store_unavailable` | Unlock or enable the OS keyring; Linux needs a Secret Service session. |
266
+ | `secret_lookup_failed` | Secret helper installation, login, exit status, nonempty stdout, and output size. |
267
+ | `connection_failed` | Server executable, working directory, endpoint, network, and TLS configuration. |
268
+ | `timeout` | Server responsiveness and the applicable request, secret-command, or OAuth time limit. |
269
+ | `protocol_error` | Server compatibility and the `protocol` setting. |
270
+ | `tool_changed` | Server filters and the current tool schema; search again. Reload Pi if connection configuration changed. |
271
+ | `tool_error` | The server's tool result and inputs; verify the outcome before retrying. |
272
+ | `oauth_failed` | Browser access to the callback and support for dynamically registered public clients. |
273
+ | `callback_unavailable` | Another process using local port 19847. |
274
+ | `busy` | Wait for discovery to finish before reconnecting. |
275
+ | `cancelled` | Retry when ready; verify any interrupted tool operation first. |
276
+ | `operation_failed` | An unclassified failure; inspect server status and configuration. |
277
+
278
+ Diagnostics never echo raw exception messages, HTTP bodies, command stderr,
279
+ credential values, or stack traces. Unknown errors stay generic rather than
280
+ being classified by potentially sensitive message text. Tool-call failures are
281
+ not replayed automatically; verify the outcome before retrying. Server-provided tool
282
+ results remain visible as content, even when the tool reports an error; they are
283
+ not sanitized transport diagnostics.
284
+
285
+ ### Large results
286
+
287
+ Text results are limited to 2,000 lines or 50 KiB. Larger results are saved as
288
+ private temporary JSON files, with their paths included in the output. Supported
289
+ images pass through within an 8 MiB base64 budget; other binary content is kept in
290
+ the full result file. Temporary result files are not automatically deleted and
291
+ may contain sensitive data.
292
+
293
+ ### v0.1 scope
294
+
295
+ The first release focuses on tools. Legacy SSE transport, MCP Apps, resource
296
+ browsing, prompt commands, roots, sampling, and elicitation are not supported.
297
+ See the [post-v0.1 backlog](https://github.com/mavam/pi-mcp-client/blob/main/TODO.md)
298
+ for follow-up work; it is not a release commitment.
299
+
300
+ ## ๐Ÿงน Uninstall
301
+
302
+ ```sh
303
+ pi remove npm:pi-mcp-client
304
+ ```
305
+
306
+ ## ๐Ÿ“„ License
307
+
308
+ [MIT](LICENSE)