Skip to content

Conversation

@yaonyan
Copy link
Contributor

@yaonyan yaonyan commented Nov 7, 2025

This pull request introduces a new "Code Execution Mode" to the MCP core, following Anthropic's MCP guidelines for efficient tool use via progressive disclosure and direct code execution. The implementation includes new executors, tool registration logic, a built-in plugin, and prompt templates, along with an example usage. It also updates the plugin system to support this new mode and fixes the usage of ajv-formats across several executors.

Major features and changes:

1. Code Execution Mode Implementation

  • New Executor: Adds CodeExecutionExecutor in code-execution-executor.ts, supporting progressive tool disclosure, efficient context/data handling, and direct JavaScript code execution with MCP tool access. It includes tracing, validation, and robust error handling.
  • Tool Registrar: Introduces registerCodeExecutionTool in code-execution-tool-registrar.ts to register code execution agents with the server, define schemas, and integrate with the new executor.
  • Built-in Plugin: Adds mode-code-execution-plugin.ts as a built-in plugin, enabling automatic registration and handling for the new mode. The plugin is integrated into the plugin system for seamless activation. [1] [2] [3] [4]
  • Prompt Template: Adds a new system prompt template for code execution mode, describing agent behavior, available operations, and JSON format for actions. [1] [2]
  • Execution Mode Type: Updates the ExecutionMode type to include "code_execution".

2. Example and Documentation

  • Example Usage: Adds 14-code-execution-mode.ts demonstrating how to use the new code execution mode with progressive disclosure, file management tools, and the new plugin.

3. Dependency and Utility Fixes

  • ajv-formats Fix: Updates all usages of ajv-formats to use addFormats.default(ajv) instead of the previous (incorrect) usage, ensuring compatibility and removing TypeScript ignore comments. [1] [2] [3]

…sage

- Add 14-code-execution-mode.ts example demonstrating progressive disclosure pattern
- Fix @ts-ignore issues with addFormats across sampling and workflow executors
- Improve JSON schema handling in agentic executor with google-compatible schemas
@yaonyan yaonyan requested a review from Copilot November 7, 2025 04:54
@yaonyan yaonyan self-assigned this Nov 7, 2025
@yaonyan yaonyan added the enhancement New feature or request label Nov 7, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new "code execution mode" for MCPC agents that implements Anthropic's progressive disclosure pattern for efficient MCP tool interaction. The key innovation is allowing agents to execute JavaScript code that can call MCP tools directly, reducing token usage by ~98.7% through on-demand tool loading and in-execution data processing.

Key changes:

  • New code_execution execution mode with progressive tool discovery and JavaScript execution
  • Support for dynamic tool search and code-based data processing
  • Fixed addFormats TypeScript import issues across multiple executors

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
packages/core/tests/executors/code_execution_test.ts Comprehensive test coverage for code execution mode including tool search, code execution, error handling, and completion
packages/core/src/prompts/types.ts Added code_execution to ExecutionMode type union
packages/core/src/prompts/index.ts Added CODE_EXECUTION system prompt with progressive disclosure documentation
packages/core/src/plugins/built-in/mode-code-execution-plugin.ts Plugin implementation for registering code execution mode tools
packages/core/src/plugins/built-in/index.ts Integrated code execution plugin into built-in plugin system
packages/core/src/executors/code-execution/code-execution-tool-registrar.ts Tool registration logic for code execution agents
packages/core/src/executors/code-execution/code-execution-executor.ts Core executor implementing tool search and code execution via Function constructor
packages/core/examples/14-code-execution-mode.ts Example demonstrating code execution mode but contains duplicate/incorrect content from example 01
packages/core/src/executors/workflow/workflow-executor.ts Fixed TypeScript error with addFormats import
packages/core/src/executors/sampling/base-sampling-executor.ts Fixed TypeScript error with addFormats import
packages/core/src/executors/agentic/agentic-executor.ts Fixed TypeScript error with addFormats import

@yaonyan yaonyan requested a review from Copilot November 8, 2025 23:48
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 7 comments.

verbose: true,
});

addFormats.default(ajv);
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addFormats import uses .default property access which suggests a CommonJS default export. This pattern may fail in some module systems. Consider using addFormats(ajv) directly if the library supports ESM, or verify that this works correctly with Deno's npm compatibility layer.

Suggested change
addFormats.default(ajv);
addFormats(ajv);

Copilot uses AI. Check for mistakes.
Comment on lines +245 to +249
const fn = new Function(
"console",
"callMCPTool",
`return (async () => { ${code} })();`,
);
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using new Function() with user-provided code creates a code injection vulnerability. While this is intentional for the feature, there should be documentation or warnings about the security implications. The code has no sandboxing, timeout mechanism, or resource limits, allowing arbitrary code execution including access to process, file system operations, and network requests.

Copilot uses AI. Check for mistakes.
yaonyan and others added 3 commits November 9, 2025 08:00
…-registrar.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Add a TODO comment explaining that using new Function() with user-provided code introduces a code injection vulnerability. This clarifies the security risk for future reviewers and maintainers.
@yaonyan yaonyan merged commit c623ce2 into main Nov 9, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants