@pi-archimedes/mcp 2.3.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 (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +170 -0
  3. package/package.json +39 -0
  4. package/src/auth-flow.test.ts +583 -0
  5. package/src/auth-flow.ts +310 -0
  6. package/src/auth-run.test.ts +309 -0
  7. package/src/auth-run.ts +146 -0
  8. package/src/auth-storage.test.ts +338 -0
  9. package/src/auth-storage.ts +330 -0
  10. package/src/auto-auth.test.ts +231 -0
  11. package/src/auto-auth.ts +135 -0
  12. package/src/callback-server.test.ts +446 -0
  13. package/src/callback-server.ts +538 -0
  14. package/src/commands-auth.test.ts +320 -0
  15. package/src/commands-auth.ts +128 -0
  16. package/src/commands.test.ts +834 -0
  17. package/src/commands.ts +424 -0
  18. package/src/config-write.test.ts +213 -0
  19. package/src/config-write.ts +207 -0
  20. package/src/config.test.ts +468 -0
  21. package/src/config.ts +278 -0
  22. package/src/direct-tools.test.ts +473 -0
  23. package/src/direct-tools.ts +250 -0
  24. package/src/host-configs.test.ts +231 -0
  25. package/src/host-configs.ts +106 -0
  26. package/src/index.test.ts +689 -0
  27. package/src/index.ts +146 -0
  28. package/src/lifecycle.test.ts +274 -0
  29. package/src/lifecycle.ts +77 -0
  30. package/src/metadata-cache.test.ts +383 -0
  31. package/src/metadata-cache.ts +231 -0
  32. package/src/npx-resolver.test.ts +142 -0
  33. package/src/npx-resolver.ts +126 -0
  34. package/src/oauth-provider.test.ts +404 -0
  35. package/src/oauth-provider.ts +197 -0
  36. package/src/oauth-types.ts +54 -0
  37. package/src/panel-rows.ts +210 -0
  38. package/src/panel.test.ts +298 -0
  39. package/src/panel.ts +742 -0
  40. package/src/proxy-tool.ts +524 -0
  41. package/src/renderer.test.ts +326 -0
  42. package/src/renderer.ts +239 -0
  43. package/src/schema-validator.test.ts +56 -0
  44. package/src/schema-validator.ts +42 -0
  45. package/src/server-client.test.ts +1001 -0
  46. package/src/server-client.ts +576 -0
  47. package/src/server-manager.ts +139 -0
  48. package/src/setup-panel.test.ts +162 -0
  49. package/src/setup-panel.ts +715 -0
  50. package/src/tool-naming.test.ts +168 -0
  51. package/src/tool-naming.ts +114 -0
  52. package/src/types.ts +162 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Daniel Cherubini and contributors
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 ADDED
@@ -0,0 +1,170 @@
1
+ # @pi-archimedes/mcp
2
+
3
+ Full-featured MCP client adapter with a pi-native TUI — feature parity with pi-mcp-adapter.
4
+
5
+ Connect any MCP server (stdio or HTTP/SSE), call its tools through a single `mcp` proxy tool or per-server direct tools, manage servers and OAuth flows interactively, and browse cached tool metadata offline — all without leaving the terminal.
6
+
7
+ ## What you get
8
+
9
+ - **`mcp` gateway tool** — search, describe, and call tools across all configured servers; also handles `status`, per-server tool listing, and eager `connect` without opening every server upfront
10
+ - **Per-server direct tools** — each server's tools registered as `{server}_{tool}` for token-efficient calls; a per-server `directTools` array narrows the set to named tools only
11
+ - **`/mcp` command family** — status, tools, prompts, reconnect, enable/disable, logout, auth, management panel, and setup panel — everything in one command namespace
12
+ - **OAuth 2.1 + PKCE** — browser auth flow for protected servers (Atlassian, Notion, GitHub, …), OS credential-store persistence, SDK-driven token refresh
13
+ - **Lifecycle management** — `keep-alive`, `lazy`, `lazy-keep-alive`, or `eager` per server, with configurable idle timeout
14
+ - **Metadata cache** — `~/.pi/agent/mcp-cache.json` (7-day validity) lets search/describe work offline and persists each server's last connection outcome so `needs-auth`/errors survive restarts
15
+ - **Compact two-line tool rendering** — `mcp <target>` header (cyan + orange) plus a key-arg summary; full args and output hidden until expanded with `ctrl+o`
16
+ - **Layered config** — six config files, lowest → highest precedence; safe single-field write-back that never touches credentials or unrelated servers
17
+
18
+ ## Install
19
+
20
+ ```bash
21
+ pi install npm:@pi-archimedes/mcp
22
+ ```
23
+
24
+ Or install the full meta package:
25
+
26
+ ```bash
27
+ pi install npm:pi-archimedes
28
+ ```
29
+
30
+ ## Quick start
31
+
32
+ 1. Create or edit a config file — the project-shared one is usually the right place:
33
+
34
+ ```bash
35
+ # in your project root
36
+ cat > .mcp.json <<'EOF'
37
+ {
38
+ "mcpServers": {
39
+ "context7": {
40
+ "command": "npx",
41
+ "args": ["-y", "@upstash/context7-mcp"]
42
+ }
43
+ }
44
+ }
45
+ EOF
46
+ ```
47
+
48
+ Or use `/mcp setup` to scaffold it interactively (see below).
49
+
50
+ 2. `/reload` to pick up the new config.
51
+
52
+ 3. The `mcp` tool and direct tools (`context7_*`) are now available. Check with `/mcp status`.
53
+
54
+ ## `/mcp` command reference
55
+
56
+ `/mcp` is the single command namespace for all MCP operations. Bare `/mcp` opens the management panel (or shows the text status list if no panel is available).
57
+
58
+ | Subcommand | Description |
59
+ |------------|-------------|
60
+ | `/mcp [status]` | One line per server: connected (tool count), needs auth, error, disabled, or not connected — persisted outcomes show an age suffix (e.g. `2m ago`) |
61
+ | `/mcp tools [server]` | Cached tools for one server or all — name + description, no connections opened |
62
+ | `/mcp prompts [server]` | Cached prompts for one server or all — name + description, no connections opened |
63
+ | `/mcp reconnect [server]` | Close and reconnect one or all servers; reports settled status per server |
64
+ | `/mcp enable <server>` | Clear the server's `disabled` flag (written to the Pi override file) — then `/reload` |
65
+ | `/mcp disable <server>` | Set `disabled` and tear down the live connection — then `/reload` |
66
+ | `/mcp logout <server>` | Delete the server's stored credentials from the OS credential store |
67
+ | `/mcp auth <server>` | Interactive OAuth flow: progress loader, browser + URL fallback, `esc` cancels |
68
+ | `/mcp panel` | Open the management panel (TUI overlay) |
69
+ | `/mcp setup` | Open the setup panel (TUI overlay) |
70
+
71
+ ## Management panel (`/mcp panel`)
72
+
73
+ Browse and act on all your servers in one overlay.
74
+
75
+ **Status glyphs:** `●` connected · `⚠` needs auth · `✗` error · `⊘` disabled · `○` cached (offline data)
76
+
77
+ | Key | Action |
78
+ |-----|--------|
79
+ | `↑` / `↓` | Move between server rows and (expanded) tool rows |
80
+ | `enter` | Expand/collapse a server's tools — on a **needs-auth** server, runs the in-panel OAuth flow instead |
81
+ | `a` | Run the in-panel OAuth flow for the focused server |
82
+ | `space` | Toggle direct tools: server row = all tools as a group, tool row = that one tool |
83
+ | `e` | Enable / disable the focused server |
84
+ | `l` | Log out (delete stored credentials) for the focused server |
85
+ | `r` | Reconnect the focused server |
86
+ | `/` | Search filter over server names and tool names/descriptions |
87
+ | `ctrl+s` | Save direct-tool changes to the Pi override file |
88
+ | `esc` | Close panel (unsaved toggles discarded); cancels an in-panel OAuth flow cleanly |
89
+
90
+ With zero servers configured, `/mcp panel` (and bare `/mcp`) notifies you and redirects to the setup panel instead.
91
+
92
+ ## Setup panel (`/mcp setup`)
93
+
94
+ Onboarding for a new project. All writes target the project-shared `.mcp.json`.
95
+
96
+ - **Scaffold** — writes `{ "mcpServers": {} }` only when the file is absent
97
+ - **Add a known server** — curated preset list (context7, chrome-devtools, deepwiki, fetch); existing entries are never overwritten
98
+ - **Import from another tool** — discovers MCP configs from Cursor, Claude Code, Claude Desktop, and VSCode; shows a preview of which server names will be added before writing; names already in `.mcp.json` are kept untouched
99
+
100
+ ## OAuth
101
+
102
+ Three paths reach the same auth entry point:
103
+
104
+ - `/mcp auth <server>` — interactive browser flow with a progress loader; opens the URL in the browser and prints it as a fallback
105
+ - In-panel — `a` key, or `enter` on a needs-auth server in `/mcp panel`; `esc` cancels cleanly
106
+ - `autoAuth: true` setting — a tool call hitting a needs-auth server triggers the flow inline and retries once
107
+
108
+ Token details:
109
+
110
+ - Tokens persist in the OS credential store (macOS Keychain / Windows Credential Manager / Linux libsecret) — no plaintext fallback
111
+ - Token refresh is SDK-driven; a pre-registered public client (`clientId` without `clientSecret`) is never auto-refreshed — re-run `/mcp auth <server>` when its token expires
112
+ - The `auth` field on http/sse servers accepts `{ "token": "…" }` (static bearer), `"oauth"` (defaults), or a full `McpOAuthConfig` object
113
+
114
+ ## Config files & write-back
115
+
116
+ Six layers load in order, lowest → highest precedence (per-server field-level merge):
117
+
118
+ | # | File | Scope |
119
+ |---|------|-------|
120
+ | 1 | `~/.config/mcp/mcp.json` | Global (standard MCP location) |
121
+ | 2 | `~/.agents/mcp.json` | Cross-agent (home) |
122
+ | 3 | `~/.agents/mcp/mcp.json` | Cross-agent (home, alternate) |
123
+ | 4 | `~/.pi/agent/mcp.json` | Pi agent directory |
124
+ | 5 | `<project>/.mcp.json` | Project-shared (committable) |
125
+ | 6 | `<project>/.pi/mcp.json` | Pi override — highest precedence |
126
+
127
+ Files accept `//` comments and trailing commas. When a higher-precedence layer changes a server's `url`, inherited `auth`/`headers`/`bearerTokenEnv` from lower layers are dropped — credentials are never sent to an endpoint you didn't explicitly configure them for.
128
+
129
+ Write-back targets:
130
+
131
+ - **`disabled` and `directTools`** → `<project>/.pi/mcp.json` (Pi override only; existing fields preserved verbatim)
132
+ - **New server definitions** (from `/mcp setup`) → `<project>/.mcp.json` (add-if-absent, never overwrites)
133
+
134
+ Changes take effect on the next `/reload`.
135
+
136
+ ## Settings
137
+
138
+ Settings are stored in `~/.pi/agent/settings.json` under the `archimedes.mcp` namespace.
139
+
140
+ | Setting | Type | Default | Description |
141
+ |---------|------|---------|-------------|
142
+ | `directTools` | bool | `true` | Register per-server direct tools (`{server}_{tool}`) in the tool list |
143
+ | `toolPrefix` | string | `"server"` | Tool name prefix strategy: `"server"` · `"none"` · `"short"` · `"mcp"` |
144
+ | `idleTimeout` | number | `10` | Minutes before idle connections close (`0` disables) |
145
+ | `autoAuth` | bool | `false` | Trigger OAuth inline on a needs-auth tool call and retry once |
146
+ | `warnOnLargeDirectTools` | bool | `true` | Reserved — parsed but not yet effective |
147
+
148
+ Per-server overrides (in the `mcp.json` server definition):
149
+
150
+ | Field | Type | Description |
151
+ |-------|------|-------------|
152
+ | `lifecycle` | string | `"keep-alive"` · `"lazy"` · `"lazy-keep-alive"` · `"eager"` (default `"lazy"`) |
153
+ | `idleTimeout` | number | Per-server idle timeout in minutes |
154
+ | `directTools` | bool \| string[] | `true` to expose all tools, or a list of tool names to expose |
155
+ | `includeTools` / `excludeTools` | string[] | Filter tools available to the `mcp` proxy |
156
+ | `toolPrefix` | string | Per-server prefix strategy |
157
+ | `disabled` | bool | Exclude from the live set without removing the definition |
158
+ | `debug` | bool | Route stdio server stderr to the terminal |
159
+ | `requestTimeoutMs` | number | Reserved — parsed but not yet effective |
160
+ | `protocolVersion` | string | Reserved — parsed but not yet effective |
161
+ | `exposeResources` | bool | Reserved — parsed but not yet effective |
162
+ | `auth` | object \| string | HTTP/SSE only — static bearer, `"oauth"`, or `McpOAuthConfig` |
163
+ | `headers` | object | HTTP/SSE only — additional request headers |
164
+ | `bearerTokenEnv` | string | HTTP/SSE only — env var name holding the bearer token |
165
+
166
+ ## Integration
167
+
168
+ When installed via `pi-archimedes` (the meta package), the MCP adapter is automatically registered. Tool rendering uses Core's chrome and color palette. Standalone installation works independently — the full feature set is available without the meta package.
169
+
170
+ ← Back to [pi-archimedes](../../README.md)
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@pi-archimedes/mcp",
3
+ "version": "2.3.0",
4
+ "type": "module",
5
+ "keywords": [
6
+ "pi-package"
7
+ ],
8
+ "description": "Full MCP client adapter with pi-native TUI rendering for pi-archimedes",
9
+ "files": [
10
+ "src"
11
+ ],
12
+ "main": "./src/index.ts",
13
+ "exports": {
14
+ ".": "./src/index.ts"
15
+ },
16
+ "dependencies": {
17
+ "@modelcontextprotocol/sdk": "^1.30.0",
18
+ "@napi-rs/keyring": "^1.3.0",
19
+ "open": "^10.2.0",
20
+ "@pi-archimedes/core": "2.3.0"
21
+ },
22
+ "peerDependencies": {
23
+ "@earendil-works/pi-coding-agent": ">=0.1.0",
24
+ "@earendil-works/pi-tui": ">=0.1.0",
25
+ "typebox": ">=1.1.0"
26
+ },
27
+ "devDependencies": {
28
+ "@earendil-works/pi-coding-agent": "^0.84.2",
29
+ "@earendil-works/pi-tui": "^0.84.2",
30
+ "@types/node": "^22.0.0",
31
+ "typebox": "^1.1.38",
32
+ "typescript": "^6.0.3"
33
+ },
34
+ "pi": {
35
+ "extensions": [
36
+ "./src/index.ts"
37
+ ]
38
+ }
39
+ }