ไธญๆๆๆกฃ | English
MCP Task Relay is a production-ready Model Context Protocol (MCP) server that exposes a sophisticated scheduler/executor workflow for autonomous agents. Built with enterprise-grade reliability, it features an intelligent Ask/Answer protocol with cryptographic context verification, preventing context drift while reducing token usage by 95%+.
โโโโโโโโโโโโโโโ MCP Protocol โโโโโโโโโโโโโโโโ
โ MCP โ โโโโโโโโโโโโโโโโโโโโบ โ Scheduler โ
โ Clients โ (stdio/SSE) โ (Relay) โ
โ (Claude/ โ โ โ
โ Codex) โ โ โโโโโโโโโโ โ
โโโโโโโโโโโโโโโ โ โAnswer โ โ
โ โRunner โ โ
โ โ(LLM) โ โ
โ โโโโโโโโโโ โ
โ โฒ โ
โ โ Ask โ
โ โผ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโ โ
โ Ask/Answer Protocol โ โContext โ โ
โ (Context Envelope) โ โEnvelopeโ โ
โ โโโโโโโโโโโโโโโโ โ โ+ Hash โ โ
โ โ Executor SDK โ โโโโโโโโโโค โโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโโ โ โ
โ Job Execution โโโโโโโโโโโโโโโโ
โ Environment
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Key Innovation: Context Envelope Protocol with SHA-256 verification ensures perfect context alignment between Executor and Answer Runner, eliminating context drift while minimizing token overhead.
Prevents context drift through explicit, verifiable context snapshots:
- Cryptographic Verification: SHA-256 hash ensures context integrity
- Token Optimization: ~50 tokens (minimal) vs 10k-50k tokens (full history)
- Zero Session Memory: Each Ask processed independently with complete context reconstruction
- Answer Attestation: Cryptographic proof of context/role/model/tools used
- Four-Layer Prompt Architecture: Base โ Role โ Context โ Task
- Role Catalog: YAML-based extensible role definitions
- LLM Integration: Anthropic Claude with automatic retry and validation
- JSON Schema Validation: Type-safe responses with schema enforcement
- Decision Caching: Eliminates redundant LLM calls for identical queries
- SQLite with WAL Mode: High-performance concurrent access
- Automatic Schema Management: No manual migrations required
- In-Memory Mode: Perfect for testing and CI/CD
- Full Audit Trail: Complete event tracking for all state transitions
- Structured Logging: JSON logs via Pino
- Real-Time Updates: Server-Sent Events (SSE) for job/ask status
- Comprehensive Metrics: Request latency, cache hit rates, token usage
- Node.js โฅ 20
- npm or bun 1.3+
- Anthropic API Key (for Answer Runner)
# NPM (recommended for production)
npm install -g mcp-task-relay
# Or use npx for one-off execution
npx -y mcp-task-relay@latest serve --profile dev# Start with in-memory storage (development)
mcp-task-relay serve \
--profile dev \
--storage memory \
--config-dir ./.mcp-task-relay
# Start with persistent storage (production)
export ANTHROPIC_API_KEY="sk-ant-..."
mcp-task-relay serve \
--profile prod \
--storage sqlite \
--sqlite ./data/relay.db \
--config-dir ./config| Flag | Description | Environment Variable |
|---|---|---|
--profile <env> |
Environment profile (dev/staging/prod) | TASK_RELAY_PROFILE |
--config-dir <dir> |
Configuration directory path | TASK_RELAY_CONFIG_DIR |
--storage <type> |
Storage backend (memory/sqlite) | TASK_RELAY_STORAGE |
--sqlite <path> |
SQLite database file path | TASK_RELAY_SQLITE_URL |
--transport <type> |
Transport protocol (stdio only in Phase 2) | TASK_RELAY_TRANSPORT |
Required:
ANTHROPIC_API_KEYโ Anthropic API key for Answer Runner
Optional:
TASK_RELAY_PROMPTS_DIRโ Custom prompts directoryTASK_RELAY_SCHEMATA_DIRโ Custom JSON schemas directoryTASK_RELAY_POLICY_FILEโ Custom policy YAML fileTASK_RELAY_ANSWER_RUNNER_ENABLEDโ Enable/disable Answer Runner (default: true)
Context Envelope (Executor-side):
TASK_RELAY_JOB_IDโ Current job identifierTASK_RELAY_STEP_IDโ Current execution stepTASK_RELAY_REPOโ Repository identifierTASK_RELAY_COMMIT_SHAโ Git commit SHATASK_RELAY_POLICY_VERSIONโ Policy versionTASK_RELAY_FACT_*โ Custom facts (e.g.,TASK_RELAY_FACT_branch=main)
.mcp-task-relay/
โโโ config.yaml # Main configuration
โโโ policy.yaml # Security policy rules
โโโ prompts/ # Role definitions
โ โโโ role.diff_planner@v1.yaml
โ โโโ role.test_planner@v1.yaml
โ โโโ role.schema_summarizer@v1.yaml
โโโ schemata/ # JSON Schemas
โโโ ask.schema.json
โโโ answer.schema.json
โโโ artifacts/
โโโ diff_plan.schema.json
โโโ test_plan.schema.json
Example config.yaml:
askAnswer:
port: 3415
longPollTimeoutSec: 25
sseHeartbeatSec: 10
runner:
enabled: true
model: claude-3-5-sonnet-20241022
maxRetries: 1
defaultTimeout: 60# Add to Codex configuration
codex mcp add task-relay -- \
mcp-task-relay serve \
--profile prod \
--storage sqlite \
--sqlite ./relay.dbAdd to your Claude Code MCP settings (~/.claude-code/mcp.json):
{
"mcpServers": {
"task-relay": {
"command": "mcp-task-relay",
"args": [
"serve",
"--profile", "prod",
"--storage", "sqlite",
"--sqlite", "./relay.db"
],
"env": {
"ANTHROPIC_API_KEY": "sk-ant-..."
}
}
}
}# Configure in Gemini workspace settings
gemini config mcp add task-relay \
--command "mcp-task-relay serve" \
--args "--profile prod --storage sqlite"The Context Envelope Protocol eliminates context drift through explicit, cryptographically verified context snapshots.
Problem: Traditional approaches require transmitting complete conversation history (10k-50k tokens), causing:
- Massive token usage and cost
- Context window limitations
- Potential context misalignment between Executor and Answer Runner
Solution: Structured context snapshots with SHA-256 verification (50-300 tokens).
| Approach | Token Usage | Context Integrity | Use Case |
|---|---|---|---|
| No Context | ~50 tokens | None | Answer Runner blind to context โ |
| Context Envelope | 50-300 tokens | Cryptographic | Optimal for 95%+ scenarios โ |
| Full History | 10k-50k tokens | Complete | Complex decisions requiring full context |
Minimal (Default Environment):
{
"job_snapshot": {},
"role": "default"
}Token Cost: ~50 tokens
Typical (Production with Custom Facts):
{
"job_snapshot": {
"repo": "github.com/user/repo",
"commit_sha": "abc123def456...",
"env_profile": "production",
"policy_version": "2.0"
},
"facts": {
"branch": "main",
"pr_number": "123"
},
"tool_caps": {
"database": {
"timeout_ms": 5000
}
},
"role": "code_reviewer"
}Token Cost: ~150-200 tokens
1. Executor builds context_envelope
โโโบ Computes SHA-256 hash โ context_hash
2. Ask sent with both context_envelope + context_hash
3. Scheduler stores Ask in database
4. Answer Runner retrieves Ask
โโโบ Verifies: computed_hash == stored_hash
โโโบ FAIL-FAST on mismatch (E_CONTEXT_MISMATCH)
5. Answer Runner generates response
โโโบ Creates attestation with context_hash
6. Answer sent back to Executor
7. Executor verifies attestation
โโโบ Ensures context_hash matches original
- E_CONTEXT_MISMATCH โ Context hash verification failed
- E_CAPS_VIOLATION โ Tool capability constraint violated
- E_NO_CONTEXT_ENVELOPE โ Required context envelope missing
The SDK automatically omits default values to minimize token usage:
repo: Omitted if "unknown" (default)commit_sha: Omitted if "unknown" (default)env_profile: Omitted if "dev" (default)policy_version: Omitted if "1.0" (default)facts: Omitted if emptytool_caps: Omitted if no tools specified
Result: 75-85% token reduction in typical scenarios.
# Clone repository
git clone https://github.com/royisme/mcp-task-relay.git
cd mcp-task-relay
# Install dependencies
npm install
# Build
npm run build
# Run tests
npm test
# Link for local development
npm linksrc/
โโโ cli.ts # CLI entry point
โโโ server.ts # Runtime bootstrap
โโโ answer-runner/ # LLM-powered answering engine
โ โโโ runner.ts # Core Answer Runner
โ โโโ role-catalog.ts # YAML role loader
โ โโโ prompt-builder.ts # 4-layer prompt architecture
โโโ core/ # Business logic
โ โโโ job-manager.ts # Job orchestration
โโโ db/ # Data persistence
โ โโโ connection.ts # SQLite setup
โ โโโ asks-repository.ts # Ask/Answer storage
โ โโโ answers-repository.ts
โโโ models/ # Type definitions
โ โโโ schemas.ts # Zod schemas
โ โโโ states.ts # State machine & error codes
โโโ sdk/ # Executor SDK
โ โโโ executor.ts # Context envelope auto-packing
โโโ services/ # HTTP/SSE services
โ โโโ ask-answer.ts # Ask/Answer API endpoints
โโโ utils/ # Shared utilities
โโโ hash.ts # Context hashing & verification
โโโ logger.ts # Structured logging
prompts/ # Built-in role catalog
schemata/ # JSON Schema definitions
Contributions are welcome! Please read our Contributing Guidelines before submitting PRs.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
MIT ยฉ 2025 Roy. See LICENSE for details.
- Anthropic โ For the Claude API and MCP specification
- Better-SQLite3 โ High-performance SQLite bindings
- Zod โ Type-safe schema validation
- Issues: GitHub Issues
- Discussions: GitHub Discussions