Skip to content

Improve MCP Tool System: Implement Two-Phase Code Generation and Validation #14

@ryantzr1

Description

@ryantzr1

🎯 Problem Statement

Currently, the MCP tool system creates new tools to be run on the system, but the code cleaning process is based on regex and relies on direct exec calls. This approach is error-prone and doesn't scale well.

Would be AMAZING if anyone could help out on this!

Current Issues:

  • Regex code cleaning: fine but first writing to a file and then checking if it's correct would be better.
  • Direct execution: Uses exec without proper validation
  • Error-prone: Little validation of code quality or parameter correctness
  • Poor scalability: Difficult to maintain and extend

💡 Proposed Solution

Implement a two-phase approach for MCP tool generation and execution:

Phase 1: Code Generation and Validation

  1. Generate tool code to separate files (Python/JS scripts)
  2. Validate and fix code before execution
  3. Ensure code quality and safety standards

Phase 2: Parameter Generation and Execution

  1. Generate execution parameters using our schema
  2. Validate parameters against tool requirements
  3. Execute scripts with correct parameters

🏗️ Architecture Overview

┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   MCP Tool      │───▶│  Code Generator  │───▶│  File Writer    │
│   Request       │    │  & Validator     │    │  (Python/JS)    │
└─────────────────┘    └──────────────────┘    └─────────────────┘
                                │
                                ▼
┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   Parameter     │◀───│  Schema-Based    │◀───│  Agent Call     │
│   Execution     │    │  Parameter Gen   │    │  for Params     │
└─────────────────┘    └──────────────────┘    └─────────────────┘

🔧 Technical Requirements

Code Generation

# Example structure for generated Python tools
class GeneratedTool:
    def __init__(self, schema, parameters):
        self.schema = schema
        self.parameters = parameters
    
    def validate_parameters(self):
        # Schema-based validation
        pass
    
    def execute(self):
        # Safe execution with validated parameters
        pass

Parameter Schema

{
  "type": "object",
  "properties": {
    "tool_name": {"type": "string"},
    "parameters": {"type": "object"},
    "validation_rules": {"type": "object"}
  },
  "required": ["tool_name", "parameters"]
}

🎯 Benefits

Scalability

  • Modular design: Each tool is a separate file
  • Easy maintenance: Individual tool updates
  • Version control: Track tool changes over time

Reliability

  • Code validation: Prevents execution of invalid code
  • Parameter validation: Ensures correct parameter types
  • Error handling: Comprehensive error management

Security

  • Sandboxed execution: Isolated tool execution
  • Code review: Generated code can be reviewed before execution
  • Access control: Granular permissions for tool execution

Developer Experience

  • Debugging: Easier to debug individual tools
  • Testing: Isolated testing of tool components
  • Documentation: Auto-generated documentation from schemas

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions