Skip to content

Add support for loading tool handlers from external files in MCP server core#5085

Merged
pelikhan merged 9 commits intomainfrom
copilot/add-js-mcp-server-tool-handlers
Nov 29, 2025
Merged

Add support for loading tool handlers from external files in MCP server core#5085
pelikhan merged 9 commits intomainfrom
copilot/add-js-mcp-server-tool-handlers

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 29, 2025

  • Understand the problem statement: Add support for tool handlers loaded from external files specified in tools.json configuration
  • Update the mcp-tools.json schema to include the optional "handler" string field
  • Modify mcp_server_core.cjs to:
    • Add a new function loadToolHandlers(tools, basePath) that loads handler functions from file paths
    • Validate that imported handlers are functions
    • Support both sync and async handlers
    • Handle ES module default export pattern (module.default)
    • Serialize handler results to MCP response format
    • Add extensive logging for handler loading process
    • Add path traversal protection for relative paths
    • Handle serialization errors (circular references) with fallback
    • Extract handler wrapper to createWrappedHandler function to avoid closure bloat
    • Add debugError method for refactored error handling with automatic Error casting
    • Add shell script (.sh) handler support using GitHub Actions convention
  • Update handleMessage and processReadBuffer to be async to support async handlers
  • Add tests for handler loading functionality in mcp_server_core.test.cjs
  • Run tests to verify changes work correctly (all 31 MCP server core tests pass)
  • Lint and verify code quality
  • Run code review and address feedback
  • Update smoke workflows to write test files under /tmp/gh-aw/agent folder
  • Recompile all workflows
Original prompt

Add support the JavaScript mcp server core to require tool handlers from files specified in the tools.json configuration file.

  • the tool object in tools.JSON may have a optimal string field "handler" which points to a file path that can be resolved by the "require" command at runtime (this feature does not use the go based bundler)

  • when initializing the server, load all the handlers and store the default exported function in the tools data structure. Check imported function is a function.

  • the handler typescript signature is

    async function handler(args: Record<string, unknown>): Promise

When a tool is called, call the imported function with the args and the response is serialized to the user. Exception is error.

Add extensive logging.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


Changeset

  • Type: patch
  • Description: Add support for loading MCP tool handlers from external files; updates mcp-tools.json schema and JavaScript MCP server core to allow a handler file path (supports sync/async handlers, ES module default exports, shell-script handlers, path traversal protection, serialization fallback, and logging). Tests and workflows updated.

AI generated by Changeset Generator



Smoke Test Summary

Test Run: 2025-11-29T17:23:26Z
Status: PASS
Summary: All smoke tests passed - GitHub MCP, file writing, bash tools, and Playwright validated

AI generated by Smoke Copilot No Firewall

Copilot AI self-assigned this Nov 29, 2025
Copilot AI and others added 2 commits November 29, 2025 15:02
…er core

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
…lization error handling

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Comment thread pkg/workflow/js/mcp_server_core.cjs Outdated

// Wrap the handler to ensure it returns the expected MCP response format
// and handles both sync and async handlers
tool.handler = async args => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot move creation of handler into own function to avoid bloating the closure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Extracted handler wrapper to createWrappedHandler(server, toolName, handlerFn) function at line 213. This avoids closure bloat by passing only the required parameters. Commit: ac2f332

Comment thread pkg/workflow/js/mcp_server_core.cjs Outdated
Copilot AI changed the title [WIP] Add support for tool handlers in tools.json configuration Add support for loading tool handlers from external files in MCP server core Nov 29, 2025
Copilot AI requested a review from pelikhan November 29, 2025 15:10
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
@pelikhan
Copy link
Copy Markdown
Collaborator

@copilot add support for handlers pointing to a shell script (.sh). In that case, use the same convention as a custom github action to populate the env vars with the inputs and read the output from the output env vars as well, just like custom actions.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 29, 2025

@copilot add support for handlers pointing to a shell script (.sh). In that case, use the same convention as a custom github action to populate the env vars with the inputs and read the output from th...

Added shell script (.sh) handler support using GitHub Actions convention:

  • Inputs passed as INPUT_* environment variables (uppercased, dashes converted to underscores)
  • Outputs read from file specified by GITHUB_OUTPUT env var (key=value format per line)
  • Returns { stdout, stderr, outputs } in MCP response format

Added 4 new tests covering shell script execution, multiple outputs, error handling, and input name conversion.

Commit: cfe72e5

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Nov 29, 2025

🤖 ALERT: Smoke Copilot No Firewall failed. ANOMALY_DETECTED. REPAIR_REQUIRED.

@github-actions
Copy link
Copy Markdown
Contributor

💥 WHOOSH! Smoke Claude springs into action on this pull request! [Panel 1 begins...]

@github-actions
Copy link
Copy Markdown
Contributor

🔮 The ancient spirits stir... Smoke Codex awakens to divine this pull request...

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Nov 29, 2025

💀 Blimey! Changeset Generator failed and walked the plank! No treasure today, matey! ☠️

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Nov 29, 2025

📰 DEVELOPING STORY: Smoke Copilot reports failed. Our correspondents are investigating the incident...

@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test Summary

Test Run: 2025-11-29T16:41:14Z
Status: PASS

💥 [THE END] — Illustrated by Smoke Claude fer issue #5085 🗺️

@github-actions
Copy link
Copy Markdown
Contributor

Latest merged PRs:

  • Update changeset generator to use codex gpt-5-mini
  • Update workflow compiler to generate secure template expansion patterns
    Tests:
  • GitHub MCP review ✅
  • File write + cat ❌ (repo policy blocks writing outside /tmp/gh-aw/agent, so /tmp/smoke-test-codex-19786425689.txt could not be created)
  • Playwright title check ✅
    Overall: FAIL

🔮 The oracle has spoken through Smoke Codex fer issue #5085 🗺️

@pelikhan
Copy link
Copy Markdown
Collaborator

@copilot update smoke agentic workflows to try to write under /tmp/gh-aw/agent folder

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 29, 2025

@copilot update smoke agentic workflows to try to write under /tmp/gh-aw/agent folder

Updated all smoke agentic workflows (smoke-claude, smoke-copilot, smoke-copilot-no-firewall, smoke-codex) to write test files under /tmp/gh-aw/agent/ folder instead of directly in /tmp/. Commit: af733c9

@pelikhan pelikhan marked this pull request as ready for review November 29, 2025 16:57
@pelikhan
Copy link
Copy Markdown
Collaborator

@copilot recompile workflows

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 29, 2025

@copilot recompile workflows

Recompiled all 89 workflows. Commit: 37c7340

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Nov 29, 2025

📰 DEVELOPING STORY: Smoke Copilot reports failed. Our correspondents are investigating the incident...

@github-actions
Copy link
Copy Markdown
Contributor

🤖 SYSTEM_INIT: Smoke Copilot No Firewall ACTIVATED. PROCESSING pull request. ALL SUBSYSTEMS ONLINE.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Nov 29, 2025

🎉 Yo ho ho! Changeset Generator found the treasure and completed successfully! ⚓💰

@github-actions
Copy link
Copy Markdown
Contributor

🔮 The ancient spirits stir... Smoke Codex awakens to divine this pull request...

@github-actions
Copy link
Copy Markdown
Contributor

💥 WHOOSH! Smoke Claude springs into action on this pull request! [Panel 1 begins...]

@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test Results

Last 2 merged PRs:

Test Results:

  • ✅ GitHub MCP: Retrieved PR data successfully
  • ✅ File Writing: Created /tmp/gh-aw/agent/smoke-test-copilot-19786978320.txt
  • ✅ Bash Tool: File verified with cat command
  • ✅ Playwright MCP: Navigated to GitHub, title contains "GitHub"

Status: PASS

🤖 DIAGNOSTIC REPORT GENERATED BY Smoke Copilot No Firewall fer issue #5085 🗺️

@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test Summary

Test Run: 2025-11-29T17:23:28Z
Status: PASS

Test Results

💥 [THE END] — Illustrated by Smoke Claude fer issue #5085 🗺️

@github-actions
Copy link
Copy Markdown
Contributor

Latest merged PRs:

  • Refactor safe output type validation into data-driven validator engine
  • Update changeset generator to use codex gpt-5-mini
    GitHub MCP: ✅
    File writing: ✅
    Bash tools: ✅
    Playwright: ✅
    Overall: PASS

🔮 The oracle has spoken through Smoke Codex fer issue #5085 🗺️

@pelikhan pelikhan merged commit 60a7352 into main Nov 29, 2025
15 checks passed
@pelikhan pelikhan deleted the copilot/add-js-mcp-server-tool-handlers branch November 29, 2025 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants