|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Commands |
| 6 | + |
| 7 | +### Build & Development |
| 8 | + |
| 9 | +```bash |
| 10 | +pnpm build # Compile TypeScript to JavaScript (tsup) |
| 11 | +pnpm build:watch # Watch mode for development |
| 12 | +pnpm check # Run prettier check + TypeScript type checking |
| 13 | +pnpm lint-fix # Auto-fix formatting with prettier |
| 14 | +``` |
| 15 | + |
| 16 | +### Running the Proxy |
| 17 | + |
| 18 | +```bash |
| 19 | +# After building, run the proxy |
| 20 | +node dist/proxy.js <remote-server-url> [port] [options] |
| 21 | + |
| 22 | +# Test client mode |
| 23 | +node dist/client.js <remote-server-url> [options] |
| 24 | +``` |
| 25 | + |
| 26 | +### Docker |
| 27 | + |
| 28 | +```bash |
| 29 | +# Build Docker image |
| 30 | +docker build -t mcp-remote:latest . |
| 31 | + |
| 32 | +# Run with Docker |
| 33 | +docker run -it mcp-remote:latest https://remote.mcp.server/sse |
| 34 | + |
| 35 | +# Run with docker-compose |
| 36 | +docker-compose run mcp-remote https://remote.mcp.server/sse |
| 37 | + |
| 38 | +# Run with persistent token storage |
| 39 | +docker run -it -v mcp-auth:/home/mcp/.mcp-auth mcp-remote:latest https://remote.mcp.server/sse |
| 40 | +``` |
| 41 | + |
| 42 | +## Architecture Overview |
| 43 | + |
| 44 | +This project acts as a bidirectional proxy between: |
| 45 | + |
| 46 | +- **Local stdio transport** (used by Claude Desktop, Cursor, Windsurf) |
| 47 | +- **Remote HTTP/SSE transport** with OAuth 2.0 authentication |
| 48 | + |
| 49 | +### Key Components |
| 50 | + |
| 51 | +1. **proxy.ts**: Main entry point that handles stdio ↔ remote communication |
| 52 | + |
| 53 | + - Reads from stdin, writes to stdout |
| 54 | + - Implements transport strategy pattern (http-first, sse-first, http-only, sse-only) |
| 55 | + - Manages OAuth authentication flow when needed |
| 56 | + |
| 57 | +2. **client.ts**: Standalone test client for debugging remote connections |
| 58 | + |
| 59 | + - Useful for testing OAuth flows outside of MCP clients |
| 60 | + - Run with `npx -p mcp-remote@latest mcp-remote-client <url>` |
| 61 | + |
| 62 | +3. **lib/coordination.ts**: OAuth flow orchestration |
| 63 | + |
| 64 | + - Spins up temporary Express server for OAuth callbacks |
| 65 | + - Handles authorization code exchange |
| 66 | + - Manages browser opening for auth |
| 67 | + |
| 68 | +4. **lib/mcp-auth-config.ts**: Token persistence and management |
| 69 | + |
| 70 | + - Stores OAuth tokens in `~/.mcp-auth/` |
| 71 | + - Automatic token refresh |
| 72 | + - Debug logging to `~/.mcp-auth/{server_hash}_debug.log` when --debug flag is used |
| 73 | + |
| 74 | +5. **lib/utils.ts**: Core transport and connection logic |
| 75 | + - Implements reconnection with exponential backoff |
| 76 | + - Handles both HTTP and SSE transports |
| 77 | + - Message proxying between transports |
| 78 | + |
| 79 | +### OAuth Flow |
| 80 | + |
| 81 | +1. If server requires auth, proxy intercepts auth error |
| 82 | +2. Spins up local Express server on configurable port (default: 3334) |
| 83 | +3. Opens browser for user authorization |
| 84 | +4. Receives callback with authorization code |
| 85 | +5. Exchanges code for tokens |
| 86 | +6. Stores tokens locally for reuse |
| 87 | +7. Automatically refreshes tokens when expired |
| 88 | + |
| 89 | +### Transport Strategies |
| 90 | + |
| 91 | +- Default behavior: Try HTTP first, fall back to SSE |
| 92 | +- Configurable via `--transport` flag |
| 93 | +- Handles connection failures and retries gracefully |
| 94 | + |
| 95 | +## Important Notes |
| 96 | + |
| 97 | +- No test suite exists - testing is manual via the client command |
| 98 | +- Uses pnpm package manager (v10.11.0) |
| 99 | +- TypeScript strict mode enabled |
| 100 | +- ES modules output only |
| 101 | +- Prettier formatting with 140 char lines, single quotes, no semicolons |
| 102 | +- OAuth tokens stored in `~/.mcp-auth/` (or `$MCP_REMOTE_CONFIG_DIR`) |
| 103 | +- Debug logs available with `--debug` flag for troubleshooting auth issues |
0 commit comments