Skip to content

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%+.

License

Notifications You must be signed in to change notification settings

royisme/mcp-task-relay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

41 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

MCP Task Relay

License: MIT Node.js

ไธญๆ–‡ๆ–‡ๆกฃ | 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%+.

Architecture Overview

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     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.


โœจ Key Features

๐Ÿ” Context Envelope Protocol

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

๐Ÿค– Intelligent Ask/Answer System

  • 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

๐Ÿ’พ Enterprise-Grade Storage

  • 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

๐Ÿ“Š Production Observability

  • 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

๐Ÿš€ Quick Start

Prerequisites

  • Node.js โ‰ฅ 20
  • npm or bun 1.3+
  • Anthropic API Key (for Answer Runner)

Installation

# 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

Basic Usage

# 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

๐Ÿ“– Configuration

CLI Options

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

Environment Variables

Required:

  • ANTHROPIC_API_KEY โ€” Anthropic API key for Answer Runner

Optional:

  • TASK_RELAY_PROMPTS_DIR โ€” Custom prompts directory
  • TASK_RELAY_SCHEMATA_DIR โ€” Custom JSON schemas directory
  • TASK_RELAY_POLICY_FILE โ€” Custom policy YAML file
  • TASK_RELAY_ANSWER_RUNNER_ENABLED โ€” Enable/disable Answer Runner (default: true)

Context Envelope (Executor-side):

  • TASK_RELAY_JOB_ID โ€” Current job identifier
  • TASK_RELAY_STEP_ID โ€” Current execution step
  • TASK_RELAY_REPO โ€” Repository identifier
  • TASK_RELAY_COMMIT_SHA โ€” Git commit SHA
  • TASK_RELAY_POLICY_VERSION โ€” Policy version
  • TASK_RELAY_FACT_* โ€” Custom facts (e.g., TASK_RELAY_FACT_branch=main)

Configuration Directory Structure

.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

๐Ÿ”ง MCP Client Integration

Codex CLI

# Add to Codex configuration
codex mcp add task-relay -- \
  mcp-task-relay serve \
  --profile prod \
  --storage sqlite \
  --sqlite ./relay.db

Claude Code (Desktop)

Add 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-..."
      }
    }
  }
}

Gemini Code Assist

# Configure in Gemini workspace settings
gemini config mcp add task-relay \
  --command "mcp-task-relay serve" \
  --args "--profile prod --storage sqlite"

๐ŸŽฏ Context Envelope Protocol

Overview

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).

Token Usage Comparison

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

Context Envelope Structure

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

Verification Flow

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

Error Codes

  • E_CONTEXT_MISMATCH โ€” Context hash verification failed
  • E_CAPS_VIOLATION โ€” Tool capability constraint violated
  • E_NO_CONTEXT_ENVELOPE โ€” Required context envelope missing

Smart Defaults (Token Optimization)

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 empty
  • tool_caps: Omitted if no tools specified

Result: 75-85% token reduction in typical scenarios.


๐Ÿ› ๏ธ Development

Build from Source

# 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 link

Project Structure

src/
โ”œโ”€โ”€ 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

๐Ÿค Contributing

Contributions are welcome! Please read our Contributing Guidelines before submitting PRs.

Development Workflow

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“„ License

MIT ยฉ 2025 Roy. See LICENSE for details.


๐Ÿ™ Acknowledgments

  • Anthropic โ€” For the Claude API and MCP specification
  • Better-SQLite3 โ€” High-performance SQLite bindings
  • Zod โ€” Type-safe schema validation

๐Ÿ“ฎ Support

About

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%+.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •