|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Toolception is a dynamic MCP (Model Context Protocol) server toolkit for runtime toolset management. It allows grouping tools into toolsets and exposing only what's needed, when it's needed—reducing prompt/tool surface area for LLMs. |
| 8 | + |
| 9 | +## Common Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Build the library |
| 13 | +npm run build |
| 14 | + |
| 15 | +# Run in watch mode during development |
| 16 | +npm run dev |
| 17 | + |
| 18 | +# Type check without emitting |
| 19 | +npm run typecheck |
| 20 | + |
| 21 | +# Run all tests |
| 22 | +npm run test |
| 23 | + |
| 24 | +# Run tests once (no watch) |
| 25 | +npm run test:run |
| 26 | + |
| 27 | +# Run tests with coverage |
| 28 | +npm run test:coverage |
| 29 | + |
| 30 | +# Run a single test file |
| 31 | +npx vitest run tests/toolRegistry.test.ts |
| 32 | + |
| 33 | +# Run smoke tests (start server in one terminal, client in another) |
| 34 | +npm run dev:server-demo |
| 35 | +npm run dev:client-demo |
| 36 | +``` |
| 37 | + |
| 38 | +## Architecture |
| 39 | + |
| 40 | +### Core Components |
| 41 | + |
| 42 | +**ServerOrchestrator** (`src/core/ServerOrchestrator.ts`) |
| 43 | +- Entry point that wires together all components |
| 44 | +- Resolves startup mode (DYNAMIC vs STATIC) from configuration |
| 45 | +- Creates ModuleResolver, DynamicToolManager, and ToolRegistry |
| 46 | +- Registers meta-tools based on mode |
| 47 | + |
| 48 | +**DynamicToolManager** (`src/core/DynamicToolManager.ts`) |
| 49 | +- Manages toolset lifecycle (enable/disable) |
| 50 | +- Enforces exposure policies (allowlist, denylist, maxActiveToolsets) |
| 51 | +- Registers tools with the MCP server |
| 52 | +- Tracks active toolsets and sends change notifications |
| 53 | + |
| 54 | +**ToolRegistry** (`src/core/ToolRegistry.ts`) |
| 55 | +- Central registry preventing tool name collisions |
| 56 | +- Handles namespacing (e.g., `toolset.toolname`) |
| 57 | +- Maps toolsets to their registered tools |
| 58 | + |
| 59 | +**ModuleResolver** (`src/mode/ModuleResolver.ts`) |
| 60 | +- Resolves tools from toolset definitions |
| 61 | +- Loads module-produced tools via moduleLoaders |
| 62 | +- Validates toolset names against catalog |
| 63 | + |
| 64 | +### Server Creation APIs |
| 65 | + |
| 66 | +Two main factory functions in `src/server/`: |
| 67 | +- `createMcpServer` - Standard server with DYNAMIC or STATIC modes |
| 68 | +- `createPermissionBasedMcpServer` - Per-client toolset access control |
| 69 | + |
| 70 | +### HTTP Transport |
| 71 | + |
| 72 | +**FastifyTransport** (`src/http/FastifyTransport.ts`) |
| 73 | +- Fastify-based HTTP transport for MCP protocol |
| 74 | +- Handles SSE streams, JSON-RPC requests |
| 75 | +- Per-client server instances in DYNAMIC mode |
| 76 | + |
| 77 | +**PermissionAwareFastifyTransport** (`src/http/PermissionAwareFastifyTransport.ts`) |
| 78 | +- Extends FastifyTransport with permission checking |
| 79 | +- Supports header-based or config-based permissions |
| 80 | + |
| 81 | +### Key Types (`src/types/index.ts`) |
| 82 | + |
| 83 | +- `McpToolDefinition` - Tool with name, description, inputSchema, handler, optional annotations |
| 84 | +- `ToolSetDefinition` - Groups tools with name, description, optional modules |
| 85 | +- `ToolSetCatalog` - Record of toolset key to definition |
| 86 | +- `ExposurePolicy` - Controls maxActiveToolsets, allowlist, denylist, namespacing |
| 87 | +- `PermissionConfig` - Header or config-based permission source |
| 88 | + |
| 89 | +### Meta-tools (DYNAMIC mode) |
| 90 | + |
| 91 | +Registered in `src/meta/registerMetaTools.ts`: |
| 92 | +- `enable_toolset` / `disable_toolset` - Activate/deactivate toolsets |
| 93 | +- `list_toolsets` / `describe_toolset` - Discovery |
| 94 | +- `list_tools` - List currently registered tools |
| 95 | + |
| 96 | +## Testing Patterns |
| 97 | + |
| 98 | +Tests use Vitest with in-memory mocks. Key patterns: |
| 99 | +- Fake MCP server in `tests/helpers/fakes.ts` |
| 100 | +- Unit tests alongside integration tests in `tests/` |
| 101 | +- Smoke E2E tests in `tests/smoke-e2e/` for manual server/client testing |
| 102 | + |
| 103 | +## Build System |
| 104 | + |
| 105 | +- Vite for bundling (`vite.config.ts`) |
| 106 | +- vite-plugin-dts for TypeScript declarations |
| 107 | +- ESM-only output (`"type": "module"`) |
| 108 | +- Node.js >= 25.3.0 required |
0 commit comments