Skip to content

Commit 6e4daa3

Browse files
committed
Merge feature/mcp-typescript-codegen: comprehensive unit test suite
Added 170 unit tests with 85% coverage across all core components: - Executor (32 tests) - MCP Aggregator (31 tests) - Security Manager (41 tests) - Auth Manager (33 tests) - Tools Coordinator (19 tests) All tests documented with TODOs for known issues. Production-ready test infrastructure in place.
2 parents c421c78 + 02220d8 commit 6e4daa3

30 files changed

Lines changed: 8664 additions & 56 deletions

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ config.json
3838
**/.mcp.*.json
3939
!**/.mcp.example.json
4040

41-
# Generated schemas
42-
generated/
41+
# Generated schemas (except example mcp.d.ts for documentation)
42+
generated/*
43+
!generated/mcp.d.ts
4344

4445
# Temporary files
4546
tmp/

AUTOMEM_AUTH_ISSUE.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# AutoMem Authorization Issue
2+
3+
## Problem
4+
AutoMem returns "Unauthorized" when called through CodeMode's MCP proxy, but works perfectly when called directly via Claude Code's MCP or curl.
5+
6+
## Verified Working
7+
```bash
8+
# Direct curl to backend - WORKS
9+
curl -X POST http://127.0.0.1:8001/memory \
10+
-H "Authorization: Bearer mem_7163ec31424b3e9d74b986811fd310aa" \
11+
-d '{"content":"test","tags":["test"]}'
12+
# ✅ Returns: {"memory_id":"...","status":"success"}
13+
14+
# Direct MCP call from Claude Code - WORKS
15+
mcp__automem__store_memory({content:"test",tags:["test"]})
16+
# ✅ Success
17+
```
18+
19+
## Not Working
20+
```javascript
21+
// Through CodeMode MCP proxy - FAILS
22+
mcp.automem.store_memory({content:"test",tags:["test"]})
23+
// ❌ Error: Unauthorized
24+
```
25+
26+
## Architecture Flow
27+
1. **User code execution** → CodeMode MCP server (`execute_code` tool)
28+
2. **CodeMode runtime** → Queues MCP call with `__mcpCallTool()`
29+
3. **Executor** → Processes queue via `mcpManager.callTool()`
30+
4. **MCPAggregator** → Routes to AutoMem connection
31+
5. **StdioClientTransport** → Spawns AutoMem MCP client process
32+
6. **AutoMem MCP client** → Reads `AUTOMEM_API_KEY` from env
33+
7. **AutoMem MCP client** → Sends `Authorization: Bearer ${AUTOMEM_API_KEY}`
34+
8. **AutoMem backend** → Validates against `AUTOMEM_API_TOKEN`
35+
36+
## Configuration Status
37+
38+
### Backend (.env)
39+
```bash
40+
AUTOMEM_API_TOKEN=mem_7163ec31424b3e9d74b986811fd310aa # ✅ Correct
41+
```
42+
43+
### CodeMode Service (.mcp.json)
44+
```json
45+
{
46+
"automem": {
47+
"env": {
48+
"AUTOMEM_ENDPOINT": "http://127.0.0.1:8001",
49+
"AUTOMEM_API_KEY": "mem_7163ec31424b3e9d74b986811fd310aa" # ✅ Correct
50+
}
51+
}
52+
}
53+
```
54+
55+
### MCP Aggregator (aggregator.ts:144)
56+
```typescript
57+
const mergedEnv = { ...process.env, ...(config.env || {}) }; // ✅ Merges env vars
58+
```
59+
60+
## Debugging Added
61+
- Added debug logging in `aggregator.ts:147-151` to log AutoMem env vars
62+
- Logs go to stderr, not visible in tool results
63+
64+
## Running Processes
65+
```bash
66+
$ ps aux | grep automem
67+
# 8 AutoMem MCP processes running! ⚠️
68+
# PIDs: 63004, 45647, 23631, 45627, 45329, 41839, 32257, 32154
69+
```
70+
71+
## Hypothesis
72+
Multiple AutoMem instances may be interfering with each other, or one of them is using incorrect/missing env vars.
73+
74+
## Next Steps
75+
1. Kill all AutoMem processes and restart clean
76+
2. Verify debug logs show correct env vars being passed
77+
3. Check if MCP client is actually receiving the env vars
78+
4. Add logging to AutoMem MCP client to see what token it's sending
79+
5. Verify StdioClientTransport is passing env correctly to child process
80+
81+
## Related Files
82+
- [services/codemode-unified/src/mcp/aggregator.ts](src/mcp/aggregator.ts:130-158)
83+
- [services/codemode-unified/src/executor.ts](src/executor.ts:600-680)
84+
- [services/codemode-unified/.mcp.json](.mcp.json:3-12)
85+
- [services/automem/app.py](../automem/app.py:171,993-1003)
86+
- [/Users/danieliser/Projects/mcp-automem/dist/index.js](https://github.com/verygoodplugins/mcp-automem)
87+
88+
## Status
89+
🔴 **BLOCKED** - Cannot use AutoMem through CodeMode until auth issue resolved

CONFIGURATION.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Configuration
2+
3+
This document explains all configuration options for the Code Mode Unified MCP server.
4+
5+
## Environment Variables
6+
7+
### CODEMODE_ENABLE_MCP_INTEGRATION
8+
9+
**Type:** `boolean` (presence check)
10+
**Default:** `false` (disabled)
11+
**Description:** Enable integration with other MCP servers
12+
13+
When enabled, the server:
14+
- Loads `.mcp.json` configuration
15+
- Connects to configured MCP servers
16+
- Makes their tools available via `mcp.*` proxy
17+
- Generates TypeScript declarations
18+
19+
**Usage:**
20+
```bash
21+
CODEMODE_ENABLE_MCP_INTEGRATION=true npx tsx src/mcp-server.ts
22+
```
23+
24+
---
25+
26+
### CODEMODE_TYPE_EXPOSURE
27+
28+
**Type:** `'on-demand' | 'auto-include'`
29+
**Default:** `'on-demand'`
30+
**Description:** Controls how TypeScript type information is exposed to agents
31+
32+
#### on-demand Mode (Default)
33+
34+
**Best for:** Production use, token efficiency
35+
**Behavior:**
36+
- Types available via `mcp://types/declarations` resource
37+
- Agent must explicitly read resource to get types
38+
- Minimal tool description, stays under token limits
39+
- Works well with smart agents that explore capabilities
40+
41+
**Tool Description:**
42+
```
43+
Execute JavaScript/TypeScript code in a sandboxed runtime environment.
44+
When MCP integration is enabled, code has access to MCP tools via the
45+
global mcp object. For TypeScript type definitions and API documentation,
46+
read the resource mcp://types/declarations before writing code.
47+
```
48+
49+
**Agent Workflow:**
50+
1. Sees execute_code tool
51+
2. Reads mcp://types/declarations resource
52+
3. Gets full type information
53+
4. Writes type-safe code
54+
55+
#### auto-include Mode
56+
57+
**Best for:** Testing, less capable agents, immediate visibility
58+
**Behavior:**
59+
- Tool list directly in execute_code description
60+
- Agent sees available tools immediately
61+
- Higher token usage per request
62+
- No need to read resource first (but still available)
63+
64+
**Tool Description:**
65+
```
66+
Execute JavaScript/TypeScript code in a sandboxed runtime environment.
67+
When MCP integration is enabled, code has access to MCP tools via the
68+
global mcp object.
69+
70+
Available MCP Tools:
71+
72+
mcp.automem:
73+
- store_memory(content, tags, importance) // Store a memory with optional...
74+
- recall_memory(query, embedding, limit, ...) // Recall memories with hybrid...
75+
- associate_memories(memory1_id, memory2_id, type) // Create an association...
76+
[... all tools listed ...]
77+
78+
mcp["sequential-thinking"]:
79+
- sequentialthinking(thought, nextThoughtNeeded, thoughtNumber) // A detailed...
80+
81+
mcp.context7:
82+
- "resolve-library-id"(libraryName) // Resolves a package/product name...
83+
[...]
84+
85+
For complete TypeScript type definitions, read the resource mcp://types/declarations.
86+
```
87+
88+
**Agent Workflow:**
89+
1. Sees execute_code tool WITH full tool list
90+
2. Already knows what's available
91+
3. Optionally reads resource for complete types
92+
4. Writes code immediately
93+
94+
**Usage:**
95+
```bash
96+
CODEMODE_TYPE_EXPOSURE=auto-include npx tsx src/mcp-server.ts
97+
```
98+
99+
---
100+
101+
## Comparison: on-demand vs auto-include
102+
103+
| Feature | on-demand | auto-include |
104+
|---------|-----------|--------------|
105+
| **Token Usage** | Low (~300 tokens) | High (~1500+ tokens) |
106+
| **Agent Discovery** | Must read resource | Immediate visibility |
107+
| **Tool List Updates** | No re-listing needed | Every tool list request |
108+
| **Best For** | Smart agents, production | Testing, visibility |
109+
| **Type Detail** | Full (via resource) | Summary + resource |
110+
111+
## Recommendations
112+
113+
### Use `on-demand` (default) when:
114+
- ✅ Deploying to production
115+
- ✅ Working with capable LLM agents (Claude, GPT-4)
116+
- ✅ Token efficiency matters
117+
- ✅ Agent can discover resources
118+
119+
### Use `auto-include` when:
120+
- ✅ Testing MCP integration
121+
- ✅ Debugging agent behavior
122+
- ✅ Working with less capable agents
123+
- ✅ You want immediate tool visibility
124+
- ✅ Experimenting with different approaches
125+
126+
## Testing Both Modes
127+
128+
### Test on-demand Mode
129+
130+
```bash
131+
# Start server (on-demand is default)
132+
CODEMODE_ENABLE_MCP_INTEGRATION=true npx tsx src/mcp-server.ts
133+
134+
# Agent should:
135+
# 1. See execute_code tool
136+
# 2. Read mcp://types/declarations resource
137+
# 3. Get full type information
138+
# 4. Write code
139+
```
140+
141+
### Test auto-include Mode
142+
143+
```bash
144+
# Start server with auto-include
145+
CODEMODE_ENABLE_MCP_INTEGRATION=true \
146+
CODEMODE_TYPE_EXPOSURE=auto-include \
147+
npx tsx src/mcp-server.ts
148+
149+
# Agent should:
150+
# 1. See execute_code tool with full tool list
151+
# 2. Immediately know what's available
152+
# 3. Optionally read resource for complete types
153+
# 4. Write code
154+
```
155+
156+
## Future Configuration Options
157+
158+
Planned environment variables:
159+
160+
- `CODEMODE_TYPE_CACHE_TTL` - How long to cache generated types
161+
- `CODEMODE_TYPE_REFRESH_INTERVAL` - Auto-regenerate types periodically
162+
- `CODEMODE_DEFAULT_RUNTIME` - Change default from QuickJS
163+
- `CODEMODE_MAX_EXECUTION_TIME` - Global timeout limit
164+
- `CODEMODE_ENABLE_PROMPTS` - Toggle prompt templates feature

0 commit comments

Comments
 (0)