QUICKSTART.mdβ’6.07 kB
# Quick Start Guide - code2mcp
Get up and running with Code Mode in 5 minutes!
## β
What You've Built
A working **Code Mode MCP Server** that:
- Converts MCP tools into TypeScript APIs
- Executes LLM-generated code in a secure sandbox
- Reduces token usage by up to 98% for complex workflows
- Hides API keys from LLM-generated code
## π Prerequisites
- β
Node.js installed
- β
Claude Code or Claude Desktop
- β
At least one MCP server to connect to (optional for testing)
## π Step 1: Test the Build
```bash
cd /Users/blaser/Documents/Projects/code2mcp
# Verify build succeeded
ls -la build/index.js
# Should show: -rwxr-xr-x ... build/index.js
```
## π§ Step 2: Configure MCP Servers (Optional)
For testing purposes, you can skip this and run with zero MCP servers. To add real servers:
Edit `src/index.ts` around line 30:
```typescript
const MCP_SERVERS: MCPServerConfig[] = [
{
name: 'example-server',
transport: 'stdio',
command: 'node',
args: ['/absolute/path/to/your-mcp-server/build/index.js'],
env: {
API_KEY: process.env.EXAMPLE_API_KEY || '',
},
},
];
```
Then rebuild:
```bash
npm run build
```
## π Step 3: Register with Claude Code
Add to your `~/.claude.json`:
```json
{
"mcpServers": {
"code2mcp": {
"command": "node",
"args": ["/Users/blaser/Documents/Projects/code2mcp/build/index.js"],
"env": {
"LOG_LEVEL": "info"
}
}
}
}
```
## π― Step 4: Test It!
### Test 1: Basic Sandbox Execution
Start Claude Code and try:
> "Use code2mcp to execute this code: `console.log('Hello from Code Mode!')`"
**Expected Output:**
```
=== Execution Logs ===
Hello from Code Mode!
=== Result ===
(undefined)
Execution time: 45ms
```
### Test 2: TypeScript Compilation
> "Execute this TypeScript code: `const x: number = 42; console.log('Answer:', x);`"
**Expected Output:**
```
=== Execution Logs ===
Answer: 42
=== Result ===
(undefined)
Execution time: 52ms
```
### Test 3: Multi-step Logic
> "Execute code that creates an array, filters it, and logs the result"
Claude might write:
```typescript
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const evens = numbers.filter(n => n % 2 === 0);
console.log('Even numbers:', evens);
console.log('Count:', evens.length);
```
## π Step 5: Connect Real MCP Servers
Once you have MCP servers configured (Step 2), Claude can call them:
**Example with a weather MCP server:**
> "Get the weather for San Francisco"
Claude writes:
```typescript
const weather = await __mcp_call('weather__get_current', {
city: 'San Francisco'
});
console.log('Temperature:', weather.temperature, 'Β°F');
console.log('Conditions:', weather.conditions);
```
## π Understanding Code Mode
### Standard MCP (Token Heavy)
```
Claude Context:
ββ Tool 1 definition (500 tokens)
ββ Tool 2 definition (500 tokens)
ββ ...50 tools... (25,000 tokens)
ββ Intermediate result 1 (10,000 tokens)
ββ Intermediate result 2 (10,000 tokens)
ββ Total: ~45,000+ tokens
```
### Code Mode (Token Light)
```
Claude Context:
ββ execute_code tool (500 tokens)
ββ Code Claude writes (2,000 tokens)
ββ Final logs only (500 tokens)
ββ Total: ~3,000 tokens
Intermediate data stays in sandbox! β‘
```
## π Development Workflow
### Watch Mode (Hot Reload)
```bash
npm run dev
```
### Build for Production
```bash
npm run build
```
### Test with MCP Inspector
```bash
npm run inspector
```
### View Logs
Logs go to stderr. When running via Claude Code, check terminal output:
```bash
# Run Claude Code from terminal to see logs
claude
```
## π Verify Installation
Check that everything is working:
```bash
# 1. Build exists
ls -la build/index.js
# 2. Dependencies installed
npm list --depth=0
# 3. TypeScript compiles
npm run build
# 4. No vulnerabilities
npm audit
```
All green? You're ready! β
## π Troubleshooting
### Build Fails
```bash
# Clean and rebuild
rm -rf build node_modules
npm install
npm run build
```
### Claude Code Doesn't See the Tool
1. Check `~/.claude.json` has correct path
2. Restart Claude Code
3. Verify build/index.js exists and is executable
### Execution Errors
- Check logs in terminal where Claude Code is running
- Verify `LOG_LEVEL=debug` in .claude.json for verbose output
### No MCP Servers Connected
- This is OK for testing! The server works with zero MCP servers
- You can test sandbox execution without any external servers
## π Next Steps
1. **Read the docs**: `DOCS/Etc/CODE_MODE_IMPLEMENTATION_PLAN.md`
2. **Connect MCP servers**: Add your existing MCP servers
3. **Explore examples**: Try complex multi-tool workflows
4. **Improve security**: Consider Deno or isolated-vm for production
## π Success Criteria
You'll know it's working when:
- β
Build completes without errors
- β
Claude Code sees the `execute_code` tool
- β
Simple console.log() executes and returns logs
- β
TypeScript code compiles and runs
- β
(Optional) MCP tools can be called via `__mcp_call()`
## π‘ Example Use Cases
Once you have MCP servers connected:
### Multi-Step Workflow
```typescript
// Fetch document from Google Drive (50KB document)
const doc = await __mcp_call('google_drive__get_document', {
documentId: 'abc123'
});
// Analyze it (stays in sandbox!)
const wordCount = doc.content.split(' ').length;
console.log('Word count:', wordCount);
// Update Salesforce (document never enters Claude's context!)
await __mcp_call('salesforce__update', {
id: 'lead-xyz',
notes: `Document analyzed: ${wordCount} words`
});
```
**Token savings: 98%!** The 50KB document never enters Claude's context.
## π Resources
- [Cloudflare Code Mode Blog](https://blog.cloudflare.com/code-mode/)
- [MCP Documentation](https://modelcontextprotocol.io/)
- [Project Documentation](DOCS/)
---
**Congratulations!** You've successfully implemented Code Mode! π
This is a **revolutionary approach** to using MCP that will transform how AI agents interact with tools.
Ready to save 98% of your tokens? Start coding! π