|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Purpose and Context |
| 6 | + |
| 7 | +This is an **educational MCP server** demonstrating the **legacy HTTP + SSE transport pattern**. It's specifically designed to teach the deprecated two-endpoint SSE approach (`GET /connect` + `POST /messages`) versus modern transports. The server implements a calculator with golden standard MCP features but uses SSE for learning purposes. |
| 8 | + |
| 9 | +## Essential Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Development workflow |
| 13 | +npm run dev # Start development server (port 1923) |
| 14 | +npm run build # TypeScript compilation |
| 15 | +npm run test # Run test suite (51 passing tests expected) |
| 16 | +npm run typecheck # TypeScript validation |
| 17 | +npm run lint # ESLint validation |
| 18 | + |
| 19 | +# Testing specific components |
| 20 | +npm run test:watch # Watch mode for development |
| 21 | +npm run test:coverage # Generate coverage report |
| 22 | +jest src/tests/calculator-server.test.ts # Run specific test file |
| 23 | + |
| 24 | +# Production and debugging |
| 25 | +npm start # Production server |
| 26 | +npm run inspector # Launch MCP Inspector for testing |
| 27 | +npm run client # Run CLI demo client |
| 28 | + |
| 29 | +# Code quality |
| 30 | +npm run format # Format with Prettier |
| 31 | +npm run ci # Full CI pipeline (lint + typecheck + test + build) |
| 32 | +``` |
| 33 | + |
| 34 | +## Core Architecture |
| 35 | + |
| 36 | +### SSE Transport Implementation |
| 37 | +The architecture centers around a **two-endpoint SSE pattern** that's fundamentally different from modern MCP transports: |
| 38 | + |
| 39 | +- **`GET /connect`**: Establishes SSE stream, creates session ID, returns endpoint info |
| 40 | +- **`POST /messages`**: Receives JSON-RPC messages, requires `sessionId` query parameter |
| 41 | +- **Session Management**: Global `transports` object maps session IDs to `SSEServerTransport` instances |
| 42 | +- **Lifecycle**: Each connection gets an isolated MCP server instance via `createCalculatorServer()` |
| 43 | + |
| 44 | +### Key Architectural Components |
| 45 | + |
| 46 | +**`src/server/index.ts`** - Express server with SSE transport |
| 47 | +- Session ID generation and cleanup |
| 48 | +- Middleware for selective JSON parsing (skips `/messages` endpoint) |
| 49 | +- CORS configuration for cross-origin SSE connections |
| 50 | +- Logging middleware for request tracing |
| 51 | + |
| 52 | +**`src/server/calculator-server.ts`** - MCP server factory |
| 53 | +- Creates fresh `McpServer` instance per session |
| 54 | +- Implements tools (calculate, demo_progress), resources (constants, history, stats), prompts |
| 55 | +- In-memory calculation history with configurable limits |
| 56 | +- Progress notifications via SSE events for `demo_progress` tool |
| 57 | + |
| 58 | +**`src/types/calculator.ts`** - Zod schemas and type definitions |
| 59 | +- Operation enums and input/output validation |
| 60 | +- Calculation history structure with `inputs[]` array format |
| 61 | +- Mathematical constants and prompt argument schemas |
| 62 | + |
| 63 | +### Session Isolation Pattern |
| 64 | +Unlike stateless transports, this SSE implementation requires **per-session state management**: |
| 65 | +- Each SSE connection creates a new MCP server instance |
| 66 | +- Calculation history is isolated per session |
| 67 | +- Session cleanup on disconnect prevents memory leaks |
| 68 | +- Transport registry enables message routing to correct session |
| 69 | + |
| 70 | +### Testing Architecture |
| 71 | +The test suite uses a hybrid approach: |
| 72 | +- **Unit tests**: `calculator-server.test.ts` with in-memory transport |
| 73 | +- **Integration tests**: `sse-integration.test.ts` with real Express server |
| 74 | +- **Test utilities**: `test-utils.ts` for common patterns |
| 75 | +- **Jest configuration**: ESM support with ts-jest, custom module mapping |
| 76 | + |
| 77 | +## Critical Implementation Details |
| 78 | + |
| 79 | +### SSE Event Handling |
| 80 | +The server sends multiple SSE event types: |
| 81 | +- `endpoint`: Initial session info with `/messages?sessionId=...` |
| 82 | +- `message`: Standard JSON-RPC responses |
| 83 | +- `progress`: Custom progress notifications from `demo_progress` tool |
| 84 | + |
| 85 | +### Parameter Evolution |
| 86 | +The calculator tool parameters have been modernized from `operation/input_1/input_2` to `op/a/b` format. History entries use `inputs: number[]` array instead of separate properties. |
| 87 | + |
| 88 | +### Golden Standard Compliance |
| 89 | +Implements the MCP learning demo golden standard with: |
| 90 | +- Core tools: `calculate`, `explain-calculation`, `generate-problems` |
| 91 | +- Extended tools: `demo_progress` (with SSE progress events), stub implementations for complex tools |
| 92 | +- Resources: `calculator://constants`, `calculator://history/*`, `calculator://stats` |
| 93 | +- Proper error handling, input validation, and progress notifications |
| 94 | + |
| 95 | +### Known Test Issues |
| 96 | +Some tests are intentionally skipped with TODO comments due to: |
| 97 | +- Prompt content expectations not matching actual template output |
| 98 | +- Client capabilities being undefined in test environment |
| 99 | +- SSE session timing issues in integration tests |
| 100 | + |
| 101 | +The main test suite should show **51 passing, 8 skipped, 0 failing** for optimal user experience. |
| 102 | + |
| 103 | +## Port Configuration |
| 104 | +Default port is **1923** (configurable via `--port` CLI argument or `PORT` environment variable). This differs from many examples that use 3000 or 8080. |
| 105 | + |
| 106 | +## Educational Value |
| 107 | +This codebase serves as a reference for understanding: |
| 108 | +- Why SSE transport was deprecated (complexity, session management, asymmetric channels) |
| 109 | +- How two-endpoint patterns work vs. modern single-endpoint approaches |
| 110 | +- Session lifecycle management in persistent connections |
| 111 | +- Progress notification patterns in SSE streams |
0 commit comments