I have a comprehensive Script programming language implementation with:
- Complete lexer, parser, and semantic analyzer
- LSP server implementation (
script-lsp) - Package manager (
manuscript) - Documentation generator, testing framework, and debugger
- Existing binary targets in
src/bin/ - Modular architecture with separate concerns
Add secure MCP server functionality to make Script the first "AI-native" programming language with built-in, secure AI assistant integration. The MCP server should expose Script's language intelligence while maintaining enterprise-grade security.
Primary Security Principle: Never execute user code - only perform static analysis.
Security Layers Required:
- Input Validation: Strict code pattern filtering, size limits, complexity detection
- Sandboxing: Isolated analysis environment with resource limits
- Path Validation: Prevent directory traversal, restrict file access
- Rate Limiting: Prevent resource exhaustion attacks
- Audit Logging: Complete logging of all AI interactions
- Session Management: Secure session handling and cleanup
src/
├── mcp/ # New MCP module
│ ├── mod.rs # Module exports
│ ├── server/ # MCP server implementation
│ │ ├── mod.rs # Main server logic
│ │ └── protocol.rs # MCP protocol types
│ ├── security/ # Security framework
│ │ ├── mod.rs # Security manager
│ │ ├── validator.rs # Input validation
│ │ └── audit.rs # Audit logging
│ ├── sandbox/ # Sandboxed analysis
│ │ ├── mod.rs # Sandbox implementation
│ │ └── analyzer.rs # Safe code analysis
│ ├── tools/ # MCP tools
│ │ ├── mod.rs # Tool registry
│ │ ├── script_analyzer.rs # Code analysis tool
│ │ ├── formatter.rs # Code formatting tool
│ │ └── documentation.rs # Documentation generator
│ └── resources/ # MCP resources
│ ├── mod.rs # Resource registry
│ └── project_files.rs # Secure file access
├── bin/
│ └── script_mcp.rs # New MCP server binary
└── lib.rs # Add mcp module export
Create the following module structure with security-first design:
src/mcp/mod.rs:
- Export all public APIs
- Feature-gate MCP functionality
- Define security configuration types
src/mcp/security/mod.rs:
- SecurityManager for session management
- Input validation and sanitization
- Rate limiting and resource control
- Audit logging system
src/mcp/sandbox/mod.rs:
- SecureSandbox for isolated analysis
- Resource limits (CPU, memory, time)
- Safe AST analysis without code execution
- Complexity metrics calculation
src/mcp/server/mod.rs:
- SecureMcpServer with full protocol support
- Transport layer (stdio/tcp) with security
- Session management and cleanup
- Tool and resource registry
Protocol Support:
- Initialize/capabilities negotiation
- Tool calling with validation
- Resource access with path restrictions
- Error handling and security violations
src/mcp/tools/script_analyzer.rs:
- Integrate with existing lexer/parser/semantic analyzer
- Provide syntax/semantic analysis results
- Generate complexity metrics
- Suggest improvements (game dev focused)
src/mcp/tools/formatter.rs:
- Code formatting with Script conventions
- Safe text transformation only
src/mcp/tools/documentation.rs:
- Extract documentation comments
- Generate structured documentation
src/mcp/resources/project_files.rs:
- Secure file access with path validation
- Read-only access to allowed directories
- File type restrictions (.script, .md, .toml, etc.)
- Project metadata generation
src/bin/script_mcp.rs:
- CLI with configuration options
- Support for strict/development/production modes
- Configuration file loading (TOML)
- Graceful shutdown handling
- Health check and statistics endpoints
Integrate with existing components:
- Use existing
Lexer,Parser,SemanticAnalyzer - Leverage existing error types and span information
- Maintain consistency with LSP server architecture
- Share configuration patterns with other tools
Cargo.toml updates:
- Add MCP server binary target
- Add required dependencies (tokio, serde, etc.)
- Create "mcp" feature flag
- Update existing dependencies as needed
Detect and block these dangerous patterns:
- File system operations:
import std.fs,delete,remove - Network operations:
http,tcp,connect - Process operations:
exec,spawn,system - Path traversal:
../,..\\ - Excessive nesting (complexity bombs)
- Code size: 100KB default, 10KB strict mode
- Analysis time: 30s default, 5s strict mode
- Memory usage: 256MB default, 64MB strict mode
- File access: Read-only to specific directories
- Network access: Disabled by default
Log all security events:
- Session creation/destruction
- Tool calls with parameters
- Resource access attempts
- Security violations
- Rate limit violations
Development Mode (script-mcp-dev.toml):
[security]
max_code_size = 1000000
max_analysis_time = "60s"
allowed_paths = ["./src", "./docs", "./examples"]Production Mode (script-mcp-prod.toml):
[security]
max_code_size = 50000
max_analysis_time = "15s"
allowed_paths = ["./src"]
audit_enabled = trueCreate comprehensive tests for:
- Security validation (malicious input rejection)
- Sandbox isolation (resource limits)
- Tool functionality (analysis accuracy)
- Resource access (path validation)
- Protocol compliance (MCP specification)
- Maintain existing architecture: Follow patterns from LSP server
- Reuse existing components: Leverage lexer, parser, semantic analyzer
- Consistent error handling: Use existing error types and spans
- Shared configuration: Similar patterns to other tools
- Testing integration: Add to existing test suite
After implementation, users should be able to:
- Start MCP server:
script-mcp --mode stdio --project-root ./my-game - Connect AI assistants (Claude, ChatGPT, etc.)
- Get secure code analysis and suggestions
- Access project files safely
- Generate documentation
- Format code according to Script conventions
The AI assistant will understand Script's:
- Syntax and semantics
- Type system (gradual typing)
- Game development patterns
- Actor model concepts
- Project structure and conventions
- ✅ MCP server starts without errors
- ✅ AI assistants can connect and communicate
- ✅ Code analysis works safely (no execution)
- ✅ Security violations are detected and logged
- ✅ Resource limits are enforced
- ✅ File access is restricted appropriately
- ✅ All tests pass
- ✅ Documentation is complete
This implementation will make Script the first programming language designed specifically for secure AI integration, providing a significant competitive advantage over other languages that only have external AI tool support.
New Files:
src/mcp/mod.rssrc/mcp/security/mod.rssrc/mcp/sandbox/mod.rssrc/mcp/server/mod.rssrc/mcp/tools/mod.rssrc/mcp/tools/script_analyzer.rssrc/mcp/tools/formatter.rssrc/mcp/tools/documentation.rssrc/mcp/resources/mod.rssrc/mcp/resources/project_files.rssrc/bin/script_mcp.rs
Modified Files:
src/lib.rs(add mcp module export)Cargo.toml(add binary target and dependencies)
Please implement this secure MCP functionality following the architecture and security requirements outlined above. Focus on security first, then functionality.