@pharaoh-so/mcp 0.1.5 → 0.2.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 (42) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/LICENSE +21 -0
  3. package/README.md +237 -13
  4. package/dist/helpers.d.ts +36 -0
  5. package/dist/helpers.js +124 -0
  6. package/dist/index.js +70 -136
  7. package/dist/inspect.d.ts +11 -0
  8. package/dist/inspect.js +45 -0
  9. package/dist/install-skills.d.ts +33 -0
  10. package/dist/install-skills.js +121 -0
  11. package/dist/proxy.d.ts +4 -0
  12. package/dist/proxy.js +14 -17
  13. package/inspect-tools.json +12 -2
  14. package/package.json +64 -32
  15. package/skills/.gitkeep +0 -0
  16. package/skills/pharaoh/SKILL.md +81 -0
  17. package/skills/pharaoh-audit-tests/SKILL.md +88 -0
  18. package/skills/pharaoh-brainstorm/SKILL.md +73 -0
  19. package/skills/pharaoh-debt/SKILL.md +33 -0
  20. package/skills/pharaoh-debug/SKILL.md +69 -0
  21. package/skills/pharaoh-execute/SKILL.md +57 -0
  22. package/skills/pharaoh-explore/SKILL.md +32 -0
  23. package/skills/pharaoh-finish/SKILL.md +79 -0
  24. package/skills/pharaoh-health/SKILL.md +36 -0
  25. package/skills/pharaoh-investigate/SKILL.md +34 -0
  26. package/skills/pharaoh-onboard/SKILL.md +32 -0
  27. package/skills/pharaoh-parallel/SKILL.md +74 -0
  28. package/skills/pharaoh-plan/SKILL.md +74 -0
  29. package/skills/pharaoh-pr/SKILL.md +52 -0
  30. package/skills/pharaoh-refactor/SKILL.md +36 -0
  31. package/skills/pharaoh-review/SKILL.md +61 -0
  32. package/skills/pharaoh-review-codex/SKILL.md +80 -0
  33. package/skills/pharaoh-review-receive/SKILL.md +81 -0
  34. package/skills/pharaoh-sessions/SKILL.md +85 -0
  35. package/skills/pharaoh-tdd/SKILL.md +104 -0
  36. package/skills/pharaoh-verify/SKILL.md +72 -0
  37. package/skills/pharaoh-wiring/SKILL.md +34 -0
  38. package/skills/pharaoh-worktree/SKILL.md +85 -0
  39. package/dist/auth.js.map +0 -1
  40. package/dist/credentials.js.map +0 -1
  41. package/dist/index.js.map +0 -1
  42. package/dist/proxy.js.map +0 -1
package/CHANGELOG.md ADDED
@@ -0,0 +1,41 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@pharaoh-so/mcp` will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.5] - 2026-03-24
9
+
10
+ ### Added
11
+ - Inspect mode (`--inspect`) for MCP registry validation and debugging
12
+ - Tool manifest JSON output for Glama and other MCP registries
13
+
14
+ ### Fixed
15
+ - Improved error messages for connection failures
16
+
17
+ ## [0.1.3] - 2026-03-23
18
+
19
+ ### Added
20
+ - `inspect-tools.json` bundled in package for offline tool schema access
21
+
22
+ ### Changed
23
+ - Updated `@modelcontextprotocol/sdk` to ^1.26.0
24
+
25
+ ## [0.1.2] - 2026-03-22
26
+
27
+ ### Fixed
28
+ - Credential file permissions set correctly on first write
29
+ - Token refresh race condition when multiple requests arrive simultaneously
30
+
31
+ ## [0.1.0] - 2026-03-20
32
+
33
+ ### Added
34
+ - Initial release
35
+ - Stdio-to-SSE proxy for headless Pharaoh MCP connections
36
+ - RFC 8628 device authorization flow
37
+ - Automatic token refresh
38
+ - Credential storage with restrictive file permissions (`0600`)
39
+ - `--server` flag for custom Pharaoh instances
40
+ - `--logout` flag to clear stored credentials
41
+ - Support for all 19 Pharaoh MCP tools
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pharaoh, Inc.
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,31 +1,60 @@
1
1
  # @pharaoh-so/mcp
2
2
 
3
- Stdio-to-SSE proxy for [Pharaoh](https://pharaoh.so) — enables Claude Code in headless environments (VPS, SSH, CI) where a browser isn't available for OAuth.
3
+ [![npm version](https://img.shields.io/npm/v/@pharaoh-so/mcp)](https://www.npmjs.com/package/@pharaoh-so/mcp)
4
+ [![license](https://img.shields.io/npm/l/@pharaoh-so/mcp)](https://github.com/Pharaoh-so/pharaoh-mcp/blob/main/LICENSE)
5
+ [![node](https://img.shields.io/node/v/@pharaoh-so/mcp)](https://nodejs.org)
6
+ [![pharaoh MCP server](https://glama.ai/mcp/servers/Pharaoh-so/pharaoh/badges/score.svg)](https://glama.ai/mcp/servers/Pharaoh-so/pharaoh)
7
+
8
+ MCP proxy for [Pharaoh](https://pharaoh.so) — maps codebases into queryable knowledge graphs for AI agents.
9
+
10
+ Pharaoh gives AI coding assistants a complete architectural map of your codebase: every function, dependency, module, and connection. Instead of reading files one at a time, your AI agent queries the knowledge graph and gets instant answers about blast radius, unused code, dependency chains, and more.
11
+
12
+ This package enables [Claude Code](https://docs.anthropic.com/en/docs/build-with-claude/claude-code) to connect to Pharaoh in **headless environments** (VPS, SSH, containers, CI) where a browser isn't available for OAuth. It acts as a stdio-to-SSE proxy, presenting itself as a local MCP server while relaying all communication to the remote Pharaoh server.
4
13
 
5
14
  ## Quick Start
6
15
 
7
- **Step 1 — Authenticate** (run in your terminal):
16
+ ### Step 1 — Authenticate
17
+
18
+ Run the proxy directly to trigger the device authorization flow:
8
19
 
9
20
  ```bash
10
21
  npx @pharaoh-so/mcp
11
22
  ```
12
23
 
13
- This shows a device code and URL. Open the URL on any device (phone, laptop) to authorize. Credentials are saved to `~/.pharaoh/credentials.json` (valid for 7 days).
24
+ This displays a device code and a URL. Open the URL on **any device** (phone, laptop, tablet) and enter the code to authorize. Credentials are saved to `~/.pharaoh/credentials.json` and remain valid for 7 days, with automatic re-authorization when they expire.
14
25
 
15
- **Step 2 — Add to Claude Code:**
26
+ ### Step 2 — Add to Claude Code
16
27
 
17
28
  ```bash
18
29
  claude mcp add pharaoh -- npx @pharaoh-so/mcp
19
30
  ```
20
31
 
21
- If you previously added pharaoh as SSE, remove it first:
32
+ Verify the connection:
33
+
34
+ ```bash
35
+ claude mcp list
36
+ ```
37
+
38
+ You should see `pharaoh` listed as a **stdio** server.
39
+
40
+ ### Switching from SSE
41
+
42
+ If you previously added Pharaoh as an SSE server, remove it first:
22
43
 
23
44
  ```bash
24
45
  claude mcp remove pharaoh
25
46
  claude mcp add pharaoh -- npx @pharaoh-so/mcp
26
47
  ```
27
48
 
28
- Verify with `claude mcp list` — it should show `pharaoh` as a **stdio** server, not SSE.
49
+ ### Desktop with Browser (Alternative)
50
+
51
+ If you have a browser available (desktop, laptop), you can connect directly via SSE instead:
52
+
53
+ ```bash
54
+ claude mcp add --transport sse pharaoh https://mcp.pharaoh.so/sse
55
+ ```
56
+
57
+ This uses OAuth in the browser and doesn't require this package.
29
58
 
30
59
  ## How It Works
31
60
 
@@ -33,18 +62,213 @@ Verify with `claude mcp list` — it should show `pharaoh` as a **stdio** server
33
62
  Claude Code ← stdio → @pharaoh-so/mcp ← SSE/HTTP → mcp.pharaoh.so
34
63
  ```
35
64
 
36
- The proxy presents itself as an MCP server to Claude Code via stdio, and relays all messages to the remote Pharaoh server over SSE. Authentication uses the RFC 8628 device authorization flow — no browser required on the machine running Claude Code.
65
+ The proxy implements the [Model Context Protocol](https://modelcontextprotocol.io/) (MCP) specification:
66
+
67
+ 1. **Claude Code** launches the proxy as a child process and communicates via **stdio** (stdin/stdout)
68
+ 2. **The proxy** authenticates with Pharaoh using stored credentials (or triggers device flow)
69
+ 3. **All MCP messages** (tool calls, responses, notifications) are relayed to the remote Pharaoh server over **SSE** (Server-Sent Events)
70
+ 4. **Pharaoh** queries the knowledge graph and returns architectural data to the proxy
71
+ 5. **The proxy** forwards responses back to Claude Code via stdio
72
+
73
+ Authentication uses [RFC 8628](https://datatracker.ietf.org/doc/html/rfc8628) (OAuth 2.0 Device Authorization Grant) — no browser is needed on the machine running Claude Code.
74
+
75
+ ## Available Tools
76
+
77
+ Once connected, Pharaoh provides 19 MCP tools organized into four categories:
78
+
79
+ ### Orient (Free)
80
+
81
+ | Tool | What it answers |
82
+ |------|----------------|
83
+ | `get_codebase_map` | What modules exist and how do they relate? |
84
+ | `get_module_context` | What does this module look like before I modify it? |
85
+ | `search_functions` | Does this function already exist somewhere? |
86
+ | `get_design_system` | What UI components and tokens already exist? |
87
+
88
+ ### Investigate (Free + Pro)
89
+
90
+ | Tool | What it answers |
91
+ |------|----------------|
92
+ | `get_blast_radius` | What breaks if I change this function/file/module? |
93
+ | `query_dependencies` | How are these two modules connected? |
94
+ | `check_reachability` | Is this function actually reachable from entry points? |
95
+ | `get_vision_docs` | Is there a PRD or spec for this? |
96
+
97
+ ### Audit (Pro)
37
98
 
38
- ## Options
99
+ | Tool | What it answers |
100
+ |------|----------------|
101
+ | `get_vision_gaps` | What's specified but not built? What's built but not specified? |
102
+ | `get_cross_repo_audit` | Are shared dependencies drifting between repos? |
103
+ | `get_consolidation_opportunities` | Where is duplicate or overlapping logic? |
104
+ | `get_unused_code` | What code is never called and safe to delete? |
105
+ | `get_test_coverage` | Which modules/functions lack test coverage? |
106
+ | `get_regression_risk` | How risky is this change to production? |
107
+
108
+ ### Manage (Free)
109
+
110
+ | Tool | What it answers |
111
+ |------|----------------|
112
+ | `request_upload` | Map a local repo without installing the GitHub App |
113
+ | `setup_environment` | Install recommended development plugins |
114
+ | `pharaoh_account` | Check plan, toggle PR Guard, trigger refresh |
115
+ | `pharaoh_feedback` | Report false positives or tool issues |
116
+ | `pharaoh_admin` | Org-level administration |
117
+
118
+ ## Inspect Mode
119
+
120
+ Use `--inspect` to dump the full tool manifest as JSON (useful for MCP registry validation and debugging):
121
+
122
+ ```bash
123
+ npx @pharaoh-so/mcp --inspect
124
+ ```
125
+
126
+ This outputs the complete list of tools with their schemas and exits immediately, without connecting to the server.
127
+
128
+ ## CLI Options
39
129
 
40
130
  ```
41
- --server <url> Pharaoh server URL (default: https://mcp.pharaoh.so)
42
- --logout Clear stored credentials and exit
43
- --help Show help
131
+ Usage: pharaoh-mcp [options]
132
+
133
+ Options:
134
+ --server <url> Pharaoh server URL (default: https://mcp.pharaoh.so)
135
+ --logout Clear stored credentials and exit
136
+ --inspect Output tool manifest as JSON and exit
137
+ --help Show help
138
+ --version Show version number
139
+ ```
140
+
141
+ ## Configuration
142
+
143
+ ### Credentials
144
+
145
+ Credentials are stored at `~/.pharaoh/credentials.json` with `0600` permissions (owner-read/write only). The file contains:
146
+
147
+ - **Access token** — used to authenticate MCP requests
148
+ - **Refresh token** — used to obtain new access tokens when they expire
149
+ - **Expiry timestamp** — tokens are refreshed automatically before expiration
150
+
151
+ To clear credentials:
152
+
153
+ ```bash
154
+ npx @pharaoh-so/mcp --logout
155
+ ```
156
+
157
+ ### Custom Server
158
+
159
+ For self-hosted Pharaoh instances or development:
160
+
161
+ ```bash
162
+ claude mcp add pharaoh -- npx @pharaoh-so/mcp --server https://your-pharaoh-instance.com
44
163
  ```
45
164
 
165
+ ### Environment Variables
166
+
167
+ | Variable | Default | Description |
168
+ |----------|---------|-------------|
169
+ | `PHARAOH_SERVER_URL` | `https://mcp.pharaoh.so` | Pharaoh server URL (alternative to `--server`) |
170
+
171
+ ## Requirements
172
+
173
+ - **Node.js** >= 18
174
+ - **Claude Code** or any MCP-compatible AI client
175
+ - A **Pharaoh account** — sign up at [pharaoh.so](https://pharaoh.so)
176
+
46
177
  ## Security
47
178
 
48
- - Credentials stored with `0600` permissions (owner-read/write only)
179
+ - Credentials are stored with restrictive file permissions (`0600` owner-read/write only)
180
+ - Authentication uses the [RFC 8628](https://datatracker.ietf.org/doc/html/rfc8628) device authorization flow — no secrets are embedded in the package
181
+ - All communication with the Pharaoh server uses HTTPS/TLS
182
+ - No source code is ever transmitted — Pharaoh maps structural metadata (function names, file paths, dependency relationships) into a knowledge graph. Your code never leaves your machine.
183
+ - Tokens expire after 7 days and are refreshed automatically
49
184
  - Only use trusted server URLs — the proxy sends your auth token to the configured server
50
- - Tokens expire after 7 days; re-authorization is automatic
185
+
186
+ ### Reporting Vulnerabilities
187
+
188
+ If you discover a security vulnerability, please report it responsibly by emailing [security@pharaoh.so](mailto:security@pharaoh.so). Do not open a public issue.
189
+
190
+ ## Troubleshooting
191
+
192
+ ### "Connection refused" or "ECONNREFUSED"
193
+
194
+ The Pharaoh server may be temporarily unavailable. Check [status.pharaoh.so](https://pharaoh.so) or try again in a few minutes.
195
+
196
+ ### "Token expired" or "401 Unauthorized"
197
+
198
+ Re-authenticate by running the proxy directly:
199
+
200
+ ```bash
201
+ npx @pharaoh-so/mcp
202
+ ```
203
+
204
+ Or clear credentials and start fresh:
205
+
206
+ ```bash
207
+ npx @pharaoh-so/mcp --logout
208
+ npx @pharaoh-so/mcp
209
+ ```
210
+
211
+ ### "pharaoh" not showing in `claude mcp list`
212
+
213
+ Make sure you added it with the correct command:
214
+
215
+ ```bash
216
+ claude mcp add pharaoh -- npx @pharaoh-so/mcp
217
+ ```
218
+
219
+ Note the `--` separator between `pharaoh` and `npx`.
220
+
221
+ ### Device code not working
222
+
223
+ - Ensure you're opening the URL on a device with browser access
224
+ - The device code expires after 15 minutes — request a new one by re-running the command
225
+ - Check that you're logged into GitHub when authorizing
226
+
227
+ ### Slow startup
228
+
229
+ The first run after installation may take a moment as npm downloads the package. Subsequent runs use the cached version. To pre-install globally:
230
+
231
+ ```bash
232
+ npm install -g @pharaoh-so/mcp
233
+ claude mcp add pharaoh -- pharaoh-mcp
234
+ ```
235
+
236
+ ## How Pharaoh Works
237
+
238
+ Pharaoh parses your repositories using [tree-sitter](https://tree-sitter.github.io/) and maps structural metadata into a [Neo4j](https://neo4j.com/) knowledge graph. The graph contains:
239
+
240
+ - **Functions** — names, signatures, complexity scores, export visibility
241
+ - **Files** — paths, module membership, language classification
242
+ - **Modules** — logical groupings detected from directory structure
243
+ - **Dependencies** — import/export relationships, call chains, module connections
244
+ - **Vision specs** — PRD/spec documents linked to implementation
245
+
246
+ No source code is stored — only structural metadata. When an AI agent queries Pharaoh, it gets architectural facts in minimal tokens, not raw code dumps. This means your AI assistant can understand your entire codebase architecture without consuming its context window reading files one by one.
247
+
248
+ ## Supported Languages
249
+
250
+ - **TypeScript / JavaScript** — full support (functions, classes, imports, exports, JSX)
251
+ - **Python** — full support (functions, classes, imports, decorators)
252
+ - More languages planned via tree-sitter grammar support
253
+
254
+ ## Contributing
255
+
256
+ Contributions are welcome. Please open an issue first to discuss what you'd like to change.
257
+
258
+ 1. Fork the repository
259
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
260
+ 3. Commit your changes (`git commit -m 'Add amazing feature'`)
261
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
262
+ 5. Open a Pull Request
263
+
264
+ ## License
265
+
266
+ [MIT](LICENSE) -- Pharaoh, Inc.
267
+
268
+ ## Links
269
+
270
+ - [Pharaoh website](https://pharaoh.so)
271
+ - [Documentation](https://docs.pharaoh.so)
272
+ - [MCP specification](https://modelcontextprotocol.io/)
273
+ - [Claude Code](https://docs.anthropic.com/en/docs/build-with-claude/claude-code)
274
+ - [Report an issue](https://github.com/Pharaoh-so/pharaoh-mcp/issues)
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Pure helper functions for the mcp-proxy CLI — no side effects on import.
3
+ * Separated from index.ts so tests can import without triggering main().
4
+ */
5
+ import type { TokenResponse } from "./auth.js";
6
+ import type { Credentials } from "./credentials.js";
7
+ /** Write one or more lines to stderr. */
8
+ export declare function printLines(...lines: string[]): void;
9
+ /** Parse CLI arguments. */
10
+ export declare function parseArgs(argv?: string[]): {
11
+ server: string;
12
+ logout: boolean;
13
+ };
14
+ export declare function printUsage(): void;
15
+ /**
16
+ * Validate that a server-supplied SSE URL shares the same origin as the configured server.
17
+ * Prevents a compromised auth response from redirecting the Bearer token to an attacker's host.
18
+ * Falls back to `${server}/sse` if the URL is missing, malformed, or cross-origin.
19
+ */
20
+ export declare function resolveSseUrl(tokenSseUrl: string | undefined, server: string): string;
21
+ /** Convert a token response to storable credentials. */
22
+ export declare function tokenToCredentials(token: TokenResponse, sseUrl: string): Credentials;
23
+ /** Format remaining TTL as human-readable string (e.g. "5d 12h"). */
24
+ export declare function formatTtl(expiresAt: string): string;
25
+ /**
26
+ * Print setup instructions for Claude Code. Called in interactive mode
27
+ * after auth completes (or when credentials already exist).
28
+ */
29
+ export declare function printSetupInstructions(): void;
30
+ /** Format a credential identity string (e.g. "alice (my-org)"). */
31
+ export declare function formatIdentity(creds: Credentials): string;
32
+ /**
33
+ * Determine if credentials are valid for proxy use.
34
+ * Returns a diagnostic message if not, null if valid.
35
+ */
36
+ export declare function validateCredentials(creds: Credentials | null): string | null;
@@ -0,0 +1,124 @@
1
+ import { isExpired } from "./credentials.js";
2
+ const DEFAULT_SERVER = "https://mcp.pharaoh.so";
3
+ /** Write one or more lines to stderr. */
4
+ export function printLines(...lines) {
5
+ process.stderr.write(lines.join("\n") + "\n");
6
+ }
7
+ /** Parse CLI arguments. */
8
+ export function parseArgs(argv = process.argv.slice(2)) {
9
+ let server = DEFAULT_SERVER;
10
+ let logout = false;
11
+ for (let i = 0; i < argv.length; i++) {
12
+ if (argv[i] === "--server" && argv[i + 1]) {
13
+ server = argv[i + 1];
14
+ i++;
15
+ }
16
+ else if (argv[i] === "--logout") {
17
+ logout = true;
18
+ }
19
+ }
20
+ // Strip trailing slash
21
+ server = server.replace(/\/+$/, "");
22
+ // Reject non-HTTPS servers (allow loopback addresses for local dev)
23
+ try {
24
+ const parsed = new URL(server);
25
+ const isLoopback = parsed.hostname === "localhost" ||
26
+ parsed.hostname === "127.0.0.1" ||
27
+ parsed.hostname === "[::1]";
28
+ if (parsed.protocol !== "https:" && !isLoopback) {
29
+ printLines(`Pharaoh: --server must use HTTPS (got ${parsed.protocol}//…). Use https:// or http://localhost for local dev.`);
30
+ process.exit(1);
31
+ }
32
+ if (parsed.protocol !== "https:" && isLoopback) {
33
+ printLines("⚠ Warning: using insecure HTTP over loopback — do not use in production.");
34
+ }
35
+ }
36
+ catch {
37
+ printLines(`Pharaoh: --server is not a valid URL: ${server}`);
38
+ process.exit(1);
39
+ }
40
+ return { server, logout };
41
+ }
42
+ export function printUsage() {
43
+ printLines("Usage: pharaoh-mcp [options]", "", "Options:", " --server <url> Pharaoh server URL (default: https://mcp.pharaoh.so)", " --logout Clear stored credentials and exit", " --install-skills Install Pharaoh skills to OpenClaw (~/.openclaw/skills/)", " --help, -h Show this help", "", "Add to Claude Code:", " claude mcp add pharaoh -- npx @pharaoh-so/mcp", "");
44
+ }
45
+ /**
46
+ * Validate that a server-supplied SSE URL shares the same origin as the configured server.
47
+ * Prevents a compromised auth response from redirecting the Bearer token to an attacker's host.
48
+ * Falls back to `${server}/sse` if the URL is missing, malformed, or cross-origin.
49
+ */
50
+ export function resolveSseUrl(tokenSseUrl, server) {
51
+ const fallback = `${server}/sse`;
52
+ if (!tokenSseUrl)
53
+ return fallback;
54
+ try {
55
+ const sseOrigin = new URL(tokenSseUrl).origin;
56
+ const serverOrigin = new URL(server).origin;
57
+ if (sseOrigin !== serverOrigin) {
58
+ printLines(`Pharaoh: ignoring cross-origin sse_url (${sseOrigin} ≠ ${serverOrigin})`);
59
+ return fallback;
60
+ }
61
+ return tokenSseUrl;
62
+ }
63
+ catch {
64
+ return fallback;
65
+ }
66
+ }
67
+ /** Convert a token response to storable credentials. */
68
+ export function tokenToCredentials(token, sseUrl) {
69
+ return {
70
+ version: 1,
71
+ access_token: token.access_token,
72
+ expires_at: new Date(Date.now() + token.expires_in * 1000).toISOString(),
73
+ expires_in: token.expires_in,
74
+ sse_url: sseUrl,
75
+ github_login: token.github_login ?? null,
76
+ tenant_name: token.tenant_name ?? null,
77
+ repos: token.repos ?? [],
78
+ };
79
+ }
80
+ /** Format remaining TTL as human-readable string (e.g. "5d 12h"). */
81
+ export function formatTtl(expiresAt) {
82
+ const remainingMs = new Date(expiresAt).getTime() - Date.now();
83
+ if (remainingMs <= 0)
84
+ return "expired";
85
+ const hours = Math.floor(remainingMs / 3_600_000);
86
+ const days = Math.floor(hours / 24);
87
+ const remHours = hours % 24;
88
+ if (days > 0)
89
+ return `${days}d ${remHours}h`;
90
+ if (hours > 0)
91
+ return `${hours}h`;
92
+ return `${Math.floor(remainingMs / 60_000)}m`;
93
+ }
94
+ /**
95
+ * Print setup instructions for Claude Code. Called in interactive mode
96
+ * after auth completes (or when credentials already exist).
97
+ */
98
+ export function printSetupInstructions() {
99
+ printLines("", "┌───────────────────────────────────────────────────────┐", "│ Next step: add Pharaoh to Claude Code │", "│ │", "│ If pharaoh is already registered (e.g. as SSE): │", "│ claude mcp remove pharaoh │", "│ │", "│ Then add as stdio proxy: │", "│ claude mcp add pharaoh -- npx @pharaoh-so/mcp │", "│ │", "│ Verify with: │", "│ claude mcp list │", "└───────────────────────────────────────────────────────┘", "");
100
+ }
101
+ /** Format a credential identity string (e.g. "alice (my-org)"). */
102
+ export function formatIdentity(creds) {
103
+ return [
104
+ creds.github_login ?? "unknown",
105
+ creds.tenant_name ? `(${creds.tenant_name})` : null,
106
+ ]
107
+ .filter(Boolean)
108
+ .join(" ");
109
+ }
110
+ /**
111
+ * Determine if credentials are valid for proxy use.
112
+ * Returns a diagnostic message if not, null if valid.
113
+ */
114
+ export function validateCredentials(creds) {
115
+ if (!creds || isExpired(creds)) {
116
+ return [
117
+ "Pharaoh: no valid credentials — cannot start proxy.",
118
+ "Run this command first to authenticate:",
119
+ " npx @pharaoh-so/mcp",
120
+ "",
121
+ ].join("\n");
122
+ }
123
+ return null;
124
+ }