|
1 | 1 | # CLAUDE.md
|
2 | 2 |
|
3 |
| -This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 3 | +This file provides guidance to Claude Code when working with the FoundryVTT MCP server repository. |
4 | 4 |
|
5 |
| -## Essential Commands |
6 |
| - |
7 |
| -### Development Workflow |
8 |
| -- `npm run build` - Compile TypeScript to JavaScript in dist/ |
9 |
| -- `npm run dev` - Run server in development mode with hot reload |
10 |
| -- `npm start` - Run production server from dist/ |
11 |
| -- `npm test` - Run test suite with Vitest |
12 |
| -- `npm run test:watch` - Run tests in watch mode |
13 |
| -- `npm run test:coverage` - Generate test coverage report |
14 |
| -- `npm run lint` - Lint TypeScript code with ESLint |
15 |
| -- `npm run test-connection` - Test connection to FoundryVTT server |
16 |
| - |
17 |
| -### Testing Commands |
18 |
| -- `fvtt launch` - Start FoundryVTT server for testing |
19 |
| -- `npm run test:e2e` - Run end-to-end tests with Playwright |
20 |
| -- `npm run test:e2e:ui` - Run tests in Playwright UI mode |
21 |
| -- `npm run test:e2e:headed` - Run tests in headed browser mode |
22 |
| -- `npm run test:e2e:debug` - Run tests in debug mode |
23 |
| -- `npm run test:e2e:report` - View Playwright test results |
24 |
| -- `tsx scripts/run-e2e-tests.ts` - Run E2E tests with server checks |
25 |
| - |
26 |
| -### Documentation |
27 |
| -- `npm run docs` - Generate TypeDoc documentation |
28 |
| -- `npm run docs:serve` - Generate and serve docs locally |
29 |
| - |
30 |
| -### Setup and Maintenance |
31 |
| -- `npm run setup` - Clean build and test connection |
32 |
| -- `npm run clean` - Remove dist/ directory |
33 |
| - |
34 |
| -## Architecture Overview |
| 5 | +## Project Overview |
35 | 6 |
|
36 | 7 | This is a **Model Context Protocol (MCP) server** that bridges AI assistants with FoundryVTT tabletop gaming software.
|
37 | 8 |
|
38 |
| -### Core Components |
39 |
| - |
40 |
| -**Main Server (`src/index.ts`)** |
41 |
| -- FoundryMCPServer class handles all MCP protocol communication |
42 |
| -- Manages tool handlers for dice rolling, actor/item searches, NPC generation |
43 |
| -- Provides resource endpoints for world data access |
44 |
| -- Implements graceful shutdown and error handling |
45 |
| - |
46 |
| -**FoundryVTT Client (`src/foundry/client.ts`)** |
47 |
| -- Handles authentication via API key or username/password |
48 |
| -- Supports both REST API module and WebSocket connections |
49 |
| -- Implements retry logic and connection testing |
50 |
| -- Provides typed interfaces for all FoundryVTT data |
51 |
| - |
52 |
| -**Configuration System (`src/config/index.ts`)** |
53 |
| -- Environment-based configuration with defaults |
54 |
| -- Validation using Zod schemas |
55 |
| -- Supports multiple authentication methods |
56 |
| - |
57 |
| -**Type Definitions (`src/foundry/types.ts`)** |
58 |
| -- Complete TypeScript interfaces for FoundryVTT entities |
59 |
| -- Actor, Item, Scene, World, and DiceRoll types |
60 |
| -- Search parameter and result types |
61 |
| - |
62 |
| -### Data Flow |
63 |
| -1. AI assistant calls MCP tool (e.g., `roll_dice`, `search_actors`) |
64 |
| -2. Server validates parameters and routes to appropriate handler |
65 |
| -3. Handler uses FoundryClient to communicate with FoundryVTT |
66 |
| -4. Results are formatted and returned as MCP response |
67 |
| -5. Resources provide read-only access to world state |
68 |
| - |
69 |
| -### Authentication |
70 |
| -- **Primary**: API key authentication with FoundryVTT REST API module |
71 |
| -- **Fallback**: Username/password authentication |
72 |
| -- Environment variables: `FOUNDRY_API_KEY` or `FOUNDRY_USERNAME`/`FOUNDRY_PASSWORD` |
73 |
| - |
74 |
| -### Testing Strategy |
75 |
| -- Unit tests for individual components using Vitest framework |
76 |
| -- Integration tests for FoundryVTT connectivity |
77 |
| -- End-to-end testing with Playwright for UI validation (headless by default) |
78 |
| -- Mocked external dependencies in test environment |
79 |
| -- Test coverage reporting with V8 |
80 |
| - |
81 |
| -**Playwright Configuration:** |
82 |
| -- **Headless Mode**: Tests run without opening browser windows by default |
83 |
| -- **Multi-Browser**: Tests run on Chromium, Firefox, and WebKit |
84 |
| -- **Auto-Server**: Automatically starts FoundryVTT server before tests |
85 |
| -- **Screenshots**: Captured on test failures for debugging |
86 |
| -- **Videos**: Recorded on failures for detailed analysis |
87 |
| - |
88 |
| -## Key Environment Variables |
89 |
| - |
90 |
| -Required: |
91 |
| -- `FOUNDRY_URL` - FoundryVTT server URL |
92 |
| - - Local: `http://localhost:30000` |
93 |
| - - Reverse Proxy: `https://dnd.lakuz.com` |
94 |
| - - Network IP: `http://192.168.1.100:30000` |
95 |
| -- `FOUNDRY_API_KEY` OR `FOUNDRY_USERNAME`/`FOUNDRY_PASSWORD` |
96 |
| - |
97 |
| -Optional: |
98 |
| -- `USE_REST_MODULE=true` - Enable REST API module features |
99 |
| -- `LOG_LEVEL=debug` - Detailed logging output |
100 |
| -- `FOUNDRY_TIMEOUT=10000` - Request timeout in milliseconds |
101 |
| - |
102 |
| -## Setup Types |
103 |
| - |
104 |
| -The setup wizard now properly handles different deployment scenarios: |
105 |
| -- **Local Development**: Auto-detects localhost:30000 and similar |
106 |
| -- **Reverse Proxy/Remote**: Prompts for custom URLs like https://dnd.lakuz.com |
107 |
| -- **Network/IP**: Supports custom IP addresses and ports |
108 |
| - |
109 |
| -## Development Notes |
110 |
| - |
111 |
| -- Uses ES modules with `.js` imports (required for MCP SDK compatibility) |
112 |
| -- Strict TypeScript configuration with comprehensive type checking |
113 |
| -- ESLint rules enforce functional programming patterns |
114 |
| -- WebSocket fallback when REST API module unavailable |
115 |
| -- Graceful degradation for missing FoundryVTT features |
116 |
| - |
117 |
| -## Testing Procedures |
118 |
| - |
119 |
| -### Unit and Integration Tests |
120 |
| -```bash |
121 |
| -# Run all tests with coverage |
122 |
| -npm test |
123 |
| - |
124 |
| -# Run tests in watch mode during development |
125 |
| -npm run test:watch |
126 |
| - |
127 |
| -# Generate coverage report |
128 |
| -npm run test:coverage |
129 |
| - |
130 |
| -# Test MCP server connection to FoundryVTT |
131 |
| -npm run test-connection |
132 |
| -``` |
133 |
| - |
134 |
| -### End-to-End Testing with Playwright |
135 |
| - |
136 |
| -**Prerequisites:** |
137 |
| -- FoundryVTT installed and accessible |
138 |
| -- Browser automation tools available |
139 |
| -- Test world/data configured |
140 |
| - |
141 |
| -**E2E Testing (Headless by Default):** |
142 |
| -```bash |
143 |
| -# Run tests in headless mode (no browser windows open) |
144 |
| -npm run test:e2e |
145 |
| - |
146 |
| -# Run with visible browser for debugging |
147 |
| -npm run test:e2e:headed |
148 |
| - |
149 |
| -# Interactive test runner with UI |
150 |
| -npm run test:e2e:ui |
151 |
| - |
152 |
| -# Debug mode with breakpoints |
153 |
| -npm run test:e2e:debug |
154 |
| - |
155 |
| -# Generate and view test reports |
156 |
| -npm run test:e2e:report |
157 |
| -``` |
| 9 | +## Essential Commands |
158 | 10 |
|
159 |
| -**Manual E2E Setup (if needed):** |
160 | 11 | ```bash
|
161 |
| -# 1. Start FoundryVTT server manually |
162 |
| -fvtt launch |
163 |
| - |
164 |
| -# 2. Wait for server to be fully ready (usually 30-60 seconds) |
165 |
| -# 3. Verify FoundryVTT is accessible at configured URL |
166 |
| - |
167 |
| -# 4. Run tests (auto-starts server by default) |
168 |
| -npm run test:e2e |
169 |
| - |
170 |
| -# Alternative: Use test runner script (includes server checks) |
171 |
| -tsx scripts/run-e2e-tests.ts |
| 12 | +npm run build # Compile TypeScript |
| 13 | +npm run dev # Development mode with hot reload |
| 14 | +npm test # Run tests |
| 15 | +npm run lint # Lint code |
| 16 | +npm run test:e2e # Run Playwright E2E tests |
| 17 | +fvtt launch # Start FoundryVTT server |
172 | 18 | ```
|
173 | 19 |
|
174 |
| -**Automated E2E Pipeline:** |
175 |
| -For CI/CD integration, tests should include: |
176 |
| -1. **Pre-test setup**: Automated FoundryVTT server startup with `fvtt launch` |
177 |
| -2. **Health check**: Wait for server readiness before test execution |
178 |
| -3. **Test execution**: Playwright tests against live FoundryVTT instance |
179 |
| -4. **Cleanup**: Graceful server shutdown and resource cleanup |
| 20 | +## Architecture |
180 | 21 |
|
181 |
| -**Test Categories:** |
182 |
| -- **Module Visibility**: Verify REST API module appears in module management |
183 |
| -- **Module Settings**: Validate module configuration options are accessible |
184 |
| -- **API Endpoints**: Test REST API endpoint accessibility and responses |
185 |
| -- **Authentication**: Validate login flows and API key usage |
186 |
| -- **Module Installation**: Verify module files and manifest are properly installed |
187 |
| -- **Issue-Specific Testing**: Targeted tests for known issues and bug reproduction |
| 22 | +- **MCP Server** (`src/index.ts`): Handles tool/resource requests from AI assistants |
| 23 | +- **FoundryVTT Client** (`src/foundry/client.ts`): Communicates with FoundryVTT via REST API or WebSocket |
| 24 | +- **Tools**: Dice rolling, actor/item searches, NPC generation |
| 25 | +- **Resources**: Read-only access to world data |
188 | 26 |
|
189 |
| -**Test Data Requirements:** |
190 |
| -- Test world with sample actors, items, and scenes |
191 |
| -- Configured user accounts with appropriate permissions |
192 |
| -- Mock data for consistent test results |
| 27 | +## Environment Variables |
193 | 28 |
|
194 |
| -### Performance Testing |
195 | 29 | ```bash
|
196 |
| -# Load testing for concurrent MCP connections |
197 |
| -npm run test:load |
198 |
| - |
199 |
| -# Memory usage profiling |
200 |
| -npm run test:memory |
201 |
| - |
202 |
| -# Response time benchmarking |
203 |
| -npm run test:benchmark |
| 30 | +FOUNDRY_URL=http://localhost:30000 # FoundryVTT server URL |
| 31 | +FOUNDRY_API_KEY=your_api_key # API authentication (preferred) |
| 32 | +# OR |
| 33 | +FOUNDRY_USERNAME=username # Username authentication |
| 34 | +FOUNDRY_PASSWORD=password # Password authentication |
204 | 35 | ```
|
205 | 36 |
|
206 |
| -### Browser Modes for E2E Testing |
207 |
| - |
208 |
| -**Headless Mode (Default):** |
209 |
| -- Fastest execution, no visual distractions |
210 |
| -- Perfect for CI/CD pipelines and automated testing |
211 |
| -- Use for regular development and verification |
212 |
| - |
213 |
| -**Headed Mode (Visual):** |
214 |
| -- Opens actual browser windows to see test execution |
215 |
| -- Useful for debugging failing tests or understanding test flow |
216 |
| -- Use when developing new tests or investigating issues |
217 |
| - |
218 |
| -**Interactive Mode (UI):** |
219 |
| -- Playwright's test runner interface with step-by-step control |
220 |
| -- Best for developing and debugging complex test scenarios |
221 |
| -- Allows pausing, stepping through, and inspecting test state |
222 |
| - |
223 |
| -**Debug Mode:** |
224 |
| -- Opens browser with developer tools and breakpoints |
225 |
| -- Enables step-by-step debugging with Playwright inspector |
226 |
| -- Essential for diagnosing complex test failures |
227 |
| - |
228 |
| -### Development Testing Workflow |
229 |
| -1. **Start FoundryVTT**: `fvtt launch` (optional - auto-started by tests) |
230 |
| -2. **Run unit tests**: `npm test` |
231 |
| -3. **Test MCP connection**: `npm run test-connection` |
232 |
| -4. **Run E2E tests**: `npm run test:e2e` (headless by default) |
233 |
| -5. **Debug if needed**: `npm run test:e2e:headed` or `npm run test:e2e:debug` |
234 |
| -6. **Review test results**: `npm run test:e2e:report` |
| 37 | +## Testing |
235 | 38 |
|
236 |
| -### Issue-Specific Testing |
| 39 | +- **Unit Tests**: `npm test` (Vitest) |
| 40 | +- **E2E Tests**: `npm run test:e2e` (Playwright, headless by default) |
| 41 | +- **Issue #7 Tests**: `npm run test:issue-7` (JSON parsing error reproduction) |
237 | 42 |
|
238 |
| -**Issue #7 - JSON Parsing Errors:** |
239 |
| -```bash |
240 |
| -# Test for JSON parsing errors specifically |
241 |
| -npm run test:issue-7 |
242 |
| - |
243 |
| -# Run with visible browser for debugging |
244 |
| -npm run test:issue-7:headed |
245 |
| - |
246 |
| -# Using Makefile |
247 |
| -make test-issue-7 |
248 |
| -make test-issue-7-headed |
249 |
| -``` |
| 43 | +## Key Notes |
250 | 44 |
|
251 |
| -This test specifically targets the malformed JSON error reported in GitHub issue #7: |
252 |
| -- Tests all REST API endpoints for JSON parsing errors |
253 |
| -- Validates array response formats |
254 |
| -- Checks for malformation patterns like missing commas, extra commas, unclosed objects |
255 |
| -- Monitors console errors related to REST API |
256 |
| -- Tests timing issues during module initialization |
257 |
| - |
258 |
| -### Available Test Files |
259 |
| -- `tests/e2e/rest-api-module.spec.ts` - Module visibility and functionality tests |
260 |
| -- `tests/e2e/module-settings.spec.ts` - Comprehensive module settings validation |
261 |
| -- `tests/e2e/issue-7-json-parsing.spec.ts` - **NEW:** Issue #7 JSON parsing error reproduction |
262 |
| -- `tests/e2e/helpers/foundry-helpers.ts` - Reusable test utilities for FoundryVTT |
263 |
| - |
264 |
| -## Reference Links |
265 |
| -- https://foundryvtt.com/api/ |
266 |
| -- https://playwright.dev/docs/intro |
| 45 | +- Uses ES modules with `.js` imports (MCP SDK requirement) |
| 46 | +- WebSocket fallback when REST API module unavailable |
| 47 | +- Playwright tests run headless by default (use `test:e2e:headed` for visual debugging) |
0 commit comments