Skip to content

Commit 8ee5150

Browse files
committed
Add CLAUDE.md for repository guidance and documentation.
1 parent 8995794 commit 8ee5150

1 file changed

Lines changed: 268 additions & 0 deletions

File tree

CLAUDE.md

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Repository Overview
6+
7+
`autobox-mcp` (previously `autobox-mcp-ts`) is a TypeScript implementation of a Model Context Protocol (MCP) server that enables Claude Desktop and Claude CLI to manage Autobox AI simulations. This is part of the larger Autobox multi-repository monorepo.
8+
9+
**Key Technology Stack:**
10+
- TypeScript 5.7 with strict type checking
11+
- Node.js 18+ with ES modules (Node16 module resolution)
12+
- Yarn 4 (Plug'n'Play) for package management
13+
- Zod for runtime schema validation
14+
- Dockerode for Docker container management
15+
- @modelcontextprotocol/sdk for MCP protocol
16+
- Jest with ts-jest for testing
17+
18+
## Essential Commands
19+
20+
### Development
21+
22+
```bash
23+
yarn dev # Run with auto-reload (tsx watch)
24+
yarn build # Compile TypeScript to dist/
25+
yarn start # Run compiled JavaScript
26+
yarn test # Run all tests
27+
yarn test:watch # Run tests in watch mode
28+
yarn test:coverage # Run tests with coverage report
29+
yarn lint # Lint TypeScript code
30+
yarn lint:fix # Fix linting issues
31+
yarn format # Format code with Prettier
32+
```
33+
34+
### Docker Operations
35+
36+
```bash
37+
./bin/docker-build # Build TypeScript + Docker image
38+
./bin/docker-run # Run MCP server in Docker container
39+
```
40+
41+
### Testing with MCP Inspector (Recommended)
42+
43+
```bash
44+
# Run without building (TypeScript directly via tsx)
45+
npx @modelcontextprotocol/inspector tsx src/index.ts
46+
47+
# Or with built JavaScript
48+
npx @modelcontextprotocol/inspector node dist/index.js
49+
```
50+
51+
Opens a web interface at `http://localhost:5173` for interactive tool testing.
52+
53+
### Running Individual Tests
54+
55+
```bash
56+
# Run specific test file
57+
yarn test tests/unit/docker/manager.test.ts
58+
59+
# Run tests matching pattern
60+
yarn test --testNamePattern="should start simulation"
61+
62+
# Run tests in specific directory
63+
yarn test tests/integration/
64+
```
65+
66+
## Architecture
67+
68+
### High-Level Component Structure
69+
70+
```
71+
src/
72+
├── index.ts # Entry point: initializes MCP server
73+
├── mcp/
74+
│ ├── server.ts # AutoboxMCPServer class (main MCP logic)
75+
│ ├── tools.ts # Tool definitions (18 MCP tools)
76+
│ └── metricsGenerator.ts # AI-assisted metrics generation
77+
├── docker/
78+
│ └── manager.ts # DockerManager class (container lifecycle)
79+
├── config/
80+
│ └── manager.ts # ConfigManager (read/write/validate configs)
81+
├── types/
82+
│ ├── simulation.ts # Zod schemas & TypeScript types
83+
│ └── index.ts # Type exports
84+
└── utils/
85+
└── logger.ts # Structured logging
86+
```
87+
88+
### Key Design Patterns
89+
90+
1. **MCP Protocol Communication**
91+
- Server communicates via stdio (JSON-RPC 2.0)
92+
- Tools are stateless functions called by Claude
93+
- All responses follow MCP SDK conventions
94+
95+
2. **Docker Container Lifecycle**
96+
- Each simulation runs in an isolated `autobox-engine` container
97+
- Containers are managed through Dockerode
98+
- Dynamic port allocation (9000+) for simulation APIs
99+
- Volume mounting for config access (`~/.autobox/config/simulations/`)
100+
101+
3. **Type Safety with Runtime Validation**
102+
- Zod schemas define both TypeScript types and runtime validation
103+
- All configs validated before passing to Docker containers
104+
- Schemas match autobox-engine-ts expectations
105+
106+
4. **Configuration Management**
107+
- Simulation configs stored in `~/.autobox/config/simulations/`
108+
- Metrics configs in `~/.autobox/config/metrics/`
109+
- AI-assisted config generation using OpenAI
110+
- Configs can be JSON or TOML (engine supports both)
111+
112+
### Critical Integration Points
113+
114+
**Environment Variables (Required):**
115+
- `OPENAI_API_KEY` - Forwarded to simulation containers for agent LLM calls
116+
- `HOST_HOME` - Required when MCP runs in Docker (for volume mounting)
117+
- `HOST_USER` - Optional, for proper file permissions
118+
119+
**Docker Socket Access:**
120+
- MCP must access `/var/run/docker.sock` to manage containers
121+
- In Docker: `-v /var/run/docker.sock:/var/run/docker.sock`
122+
123+
**Volume Mounts:**
124+
- `${HOME}/.autobox` must be accessible to both MCP and simulation containers
125+
- MCP reads configs, engine containers read them at startup
126+
127+
### MCP Tool Categories
128+
129+
**Simulation Lifecycle (8 tools):**
130+
- `list_simulations`, `start_simulation`, `stop_simulation`, `stop_all_simulations`
131+
- `get_simulation_status`, `get_simulation_logs`, `get_simulation_metrics`
132+
- `abort_simulation`
133+
134+
**Configuration Management (4 tools):**
135+
- `list_available_configs`, `create_simulation_config`
136+
- `create_simulation_metrics`, `delete_simulation`
137+
138+
**Runtime Control (3 tools):**
139+
- `instruct_agent` - Send instructions to agents during simulation
140+
- `ping_simulation`, `get_simulation_health` - Health checks
141+
142+
**API Inspection (3 tools):**
143+
- `get_simulation_execution_status`, `get_simulation_info`
144+
- `get_simulation_api_spec`
145+
146+
### Build and Packaging
147+
148+
**Build Process (`./bin/docker-build`):**
149+
1. Compile TypeScript: `yarn build``dist/`
150+
2. Build Docker image with compiled code
151+
3. Image includes docker-cli for container management
152+
4. Uses Yarn 4 PnP (Plug'n'Play) in container
153+
154+
**Docker Image Details:**
155+
- Base: `node:18-alpine`
156+
- Includes: docker-cli, Yarn 4, compiled dist/
157+
- Entrypoint: `yarn node dist/index.js`
158+
- Requires: `/var/run/docker.sock` mount
159+
160+
## Testing Strategy
161+
162+
### Test Organization
163+
164+
```
165+
tests/
166+
├── unit/ # Isolated component tests
167+
│ ├── config/ # ConfigManager tests
168+
│ ├── docker/ # DockerManager tests
169+
│ └── mcp/ # MCP server tests
170+
└── integration/ # End-to-end tests with Docker
171+
```
172+
173+
### Jest Configuration Notes
174+
175+
- Uses `ts-jest` with ESM preset
176+
- ES modules enabled (`extensionsToTreatAsEsm: ['.ts']`)
177+
- Coverage collected from `src/**/*.ts`
178+
- Test files: `**/*.test.ts` or `**/*.spec.ts`
179+
180+
### Running Tests Effectively
181+
182+
```bash
183+
# Fast feedback loop
184+
yarn test:watch
185+
186+
# Coverage to identify untested code
187+
yarn test:coverage
188+
189+
# Integration tests (requires Docker)
190+
yarn test tests/integration/
191+
192+
# Debug specific test
193+
node --inspect-brk node_modules/.bin/jest tests/unit/docker/manager.test.ts
194+
```
195+
196+
## Common Development Patterns
197+
198+
### Adding a New MCP Tool
199+
200+
1. Add tool definition to `src/mcp/tools.ts` (name, description, inputSchema)
201+
2. Implement handler in `AutoboxMCPServer.setupToolHandlers()` in `src/mcp/server.ts`
202+
3. Add Zod schema if new types are needed in `src/types/`
203+
4. Write unit tests in `tests/unit/mcp/`
204+
5. Update README.md tool list
205+
206+
### Modifying Simulation Config Schema
207+
208+
1. Update Zod schema in `src/types/simulation.ts`
209+
2. TypeScript types auto-infer from schema
210+
3. Ensure compatibility with autobox-engine-ts schemas
211+
4. Test with `SimulationConfigSchema.parse(testData)`
212+
213+
### Docker Manager Operations
214+
215+
All Docker interactions go through `DockerManager`:
216+
- `startSimulation()` - Creates and starts container
217+
- `stopSimulation()` - Gracefully stops container
218+
- `getSimulationStatus()` - Queries container state
219+
- `getSimulationLogs()` - Retrieves container logs
220+
- `listSimulations()` - Lists all autobox containers
221+
222+
## Dependencies on Other Autobox Components
223+
224+
**Critical Dependency:**
225+
- **autobox-engine-ts** Docker image must exist (`autobox-engine:latest`)
226+
- Built from `../autobox-engine-ts` via `./bin/docker-build`
227+
- MCP starts this image for each simulation
228+
229+
**Shared Contracts:**
230+
- Simulation config schema must match engine expectations
231+
- API endpoints (`/health`, `/status`, `/agents/instruct`) defined by engine
232+
- Metrics structure defined by engine's FastAPI server
233+
234+
## Troubleshooting Guide
235+
236+
**"Cannot connect to Docker daemon":**
237+
- Ensure Docker Desktop is running
238+
- Check `/var/run/docker.sock` permissions
239+
- On Linux: `sudo usermod -aG docker $USER` and re-login
240+
241+
**"No such file or directory" loading configs:**
242+
- Set `HOST_HOME` environment variable when running MCP in Docker
243+
- Verify `~/.autobox/config/simulations/` exists and contains configs
244+
- Check volume mount syntax in Docker run command
245+
246+
**"401 You didn't provide an API key":**
247+
- Set `OPENAI_API_KEY` before starting MCP
248+
- Verify it's passed to Docker: `-e OPENAI_API_KEY=${OPENAI_API_KEY}`
249+
- Check OpenAI API key validity at platform.openai.com
250+
251+
**MCP tools not appearing in Claude:**
252+
- Rebuild Docker image: `./bin/docker-build`
253+
- Verify `claude_desktop_config.json` has correct Docker command
254+
- Restart Claude Desktop completely (Cmd+Q and reopen)
255+
- Check Claude logs: `~/Library/Logs/Claude/mcp*.log`
256+
257+
**TypeScript errors after pulling changes:**
258+
- Delete `node_modules` and reinstall: `yarn install`
259+
- Clean build: `rm -rf dist && yarn build`
260+
- Check Node.js version: `node --version` (must be 18+)
261+
262+
## Development Tips
263+
264+
1. **Fast Iteration**: Use `yarn dev` + MCP Inspector for instant feedback
265+
2. **Type Checking**: Run `tsc --noEmit` to check types without building
266+
3. **Debug Logging**: Set `LOG_LEVEL=debug` environment variable
267+
4. **Container Cleanup**: Use `docker ps -a | grep autobox` to find orphaned containers
268+
5. **Config Validation**: Test configs with `SimulationConfigSchema.parse()` before running

0 commit comments

Comments
 (0)