Add script middleware with config toggle for code mode#4821
Draft
Add script middleware with config toggle for code mode#4821
Conversation
Add configuration type for the script execution engine (code mode). ScriptEngineConfig controls whether execute_tool_script is available, with tuning parameters for step limits and parallel concurrency. Part of #4742 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Minimal http.ResponseWriter implementation that captures status, headers, and body into a buffer. Used by the script middleware to dispatch inner tool calls through the middleware chain without importing net/http/httptest in production code. Part of #4742 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
HTTP middleware that integrates the script engine into the vMCP request pipeline: - Intercepts tools/call for execute_tool_script: extracts script and data arguments, constructs an Executor with tool bindings that route inner calls through the middleware chain, returns the aggregated result as a JSON-RPC response. - Intercepts tools/list: appends the execute_tool_script virtual tool definition with a dynamic description listing all available tools. - Uses ParsedMCPRequest from context to inspect requests without re-reading the body. - Inner tool calls dispatch through next handler via responseCapture, clearing the parsed request context so the MCP parser re-parses the synthetic request. Part of #4742 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add ScriptMiddleware field to server Config and insert it into the middleware chain after authz. Inner tool calls from scripts flow through the full chain (discovery, authz, etc.). Wire createScriptMiddleware in commands.go: when config.ScriptEngine.Enabled is true, creates the middleware with the configured step limit and parallel concurrency; otherwise nil (no virtual tool exposed). Part of #4742 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Table-driven tests covering: - tools/list injects execute_tool_script when middleware is active - Script execution calls multiple backend tools and returns aggregated result - Non-script tools/call passes through to backend unchanged - Missing, non-string, and syntactically invalid script arguments return appropriate JSON-RPC errors - Non-MCP methods pass through unchanged - Config toggle: no middleware → no virtual tool; with middleware → virtual tool present Part of #4742 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Generated artifacts for the new ScriptEngineConfig type: - docs/operator/crd-api.md — CRD API reference documentation - pkg/vmcp/config/zz_generated.deepcopy.go — deepcopy methods - deploy/charts/operator-crds/ — CRD manifests with scriptEngine field - cmd/vmcp/app/commands.go — import ordering fix (gci) Part of #4742 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4821 +/- ##
==========================================
- Coverage 69.05% 68.97% -0.08%
==========================================
Files 530 532 +2
Lines 55375 55523 +148
==========================================
+ Hits 38238 38297 +59
- Misses 14193 14258 +65
- Partials 2944 2968 +24 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ScriptEngineconfig toggle (disabled by default) that controls whether theexecute_tool_scriptvirtual tool is available. When enabled, the middleware interceptstools/callfor script execution and injects the virtual tool intotools/listresponses.Part of #4742
Type of change
Test plan
task test)task lint-fix)Changes
pkg/vmcp/config/config.goScriptEngineConfigtype andScriptEnginefield to Configpkg/script/response_capture.gohttp.ResponseWriterfor capturing inner tool call responsespkg/script/middleware.gotools/listinjection,tools/callinterception, inner tool dispatchpkg/script/middleware_test.gopkg/vmcp/server/server.goScriptMiddlewareto Config, insert in Handler() chain after authzcmd/vmcp/app/commands.gocreateScriptMiddlewarefrom configDoes this introduce a user-facing change?
No. The config toggle defaults to disabled. This becomes user-facing when CRD types are added in PR 4.
Special notes for reviewers
Review by commit — each commit introduces one concern:
ScriptEngineConfig)httptest.NewRecorderin production)Inner tool dispatch: Scripts call tools through
next.ServeHTTP(capture, innerReq)wherecaptureis a purpose-builtresponseCapturewriter. The parsed MCP request is cleared from context so the parser re-parses the synthetic request. This ensures inner calls flow through the full middleware chain (authz, discovery, etc.).Middleware chain placement: Script middleware wraps after authz, so scripts only see authorized tools and inner tool calls are subject to authorization.
Generated with Claude Code