Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 18, 2025

Summary

Stdio server panics with "ToolDependencies not found in context" on tool calls. After deps were refactored to context-based injection, middleware to inject them was missing.

Why

Tool handlers call MustDepsFromContext(ctx) but no middleware injects deps into the request context.

What changed

  • Added middleware in NewMCPServer() that wraps requests with github.ContextWithDeps(ctx, deps)
  • Added tests for server creation and toolset resolution logic
// Inject dependencies into context for all tool handlers
ghServer.AddReceivingMiddleware(func(next mcp.MethodHandler) mcp.MethodHandler {
    return func(ctx context.Context, method string, req mcp.Request) (mcp.Result, error) {
        return next(github.ContextWithDeps(ctx, deps), method, req)
    }
})

MCP impact

  • No tool or API changes — Fixes runtime panic, no API surface changes

Prompts tested (tool changes only)

  • N/A

Security / limits

  • No security or limits impact — Middleware only propagates existing deps

Lint & tests

  • Linted locally with ./script/lint
  • Tested locally with ./script/test

Docs

  • Not needed — Internal fix, no user-facing changes
Original prompt

This section details on the original issue you should resolve

<issue_title>v0.26.0+ stdio mode panics: ToolDependencies not found in context</issue_title>
<issue_description>## Bug Description

After PR #1640 (inject deps via context instead of closures), the stdio server panics on any tool call:

panic: ToolDependencies not found in context; use ContextWithDeps to inject

Root Cause

In internal/ghmcp/server.go, NewMCPServer() creates deps at line ~196-204 and passes them to inventory.RegisterAll(), but the new context-based tool handlers created by NewServerToolWithContextHandler expect deps to be in context via MustDepsFromContext(ctx).

No middleware injects deps into context before tool handlers execute.

Fix

Add middleware after deps creation (~line 205):

ghServer.AddReceivingMiddleware(func(next mcp.MethodHandler) mcp.MethodHandler {
    return func(ctx context.Context, method string, req mcp.Request) (mcp.Result, error) {
        return next(github.ContextWithDeps(ctx, deps), method, req)
    }
})

Affected Versions

  • v0.26.0
  • v0.26.1

Reproduction

export GITHUB_PERSONAL_ACCESS_TOKEN=$TOKEN
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"0.1.0","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"get_me","arguments":{}}}' | ./github-mcp-server stdio

Workaround

Build from source with the above fix applied.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

The issue was that after PR #1640 switched from closure-based deps to context-based deps,
the stdio server was missing middleware to inject ToolDependencies into the request context.
This caused tools to panic with "ToolDependencies not found in context" when called.

Added middleware in NewMCPServer() that wraps all requests with github.ContextWithDeps(),
ensuring deps are available to tool handlers via MustDepsFromContext().

Also added tests to verify server creation and toolset resolution logic.

Co-authored-by: SamMorrowDrums <4811358+SamMorrowDrums@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix stdio mode panics for tool dependencies Fix stdio mode panic: inject ToolDependencies into request context Dec 18, 2025
Copilot AI requested a review from SamMorrowDrums December 18, 2025 20:47
@SamMorrowDrums SamMorrowDrums marked this pull request as ready for review December 18, 2025 20:50
@SamMorrowDrums SamMorrowDrums requested a review from a team as a code owner December 18, 2025 20:50
Copilot AI review requested due to automatic review settings December 18, 2025 20:50
@SamMorrowDrums SamMorrowDrums merged commit 311d581 into main Dec 18, 2025
19 checks passed
@SamMorrowDrums SamMorrowDrums deleted the copilot/fix-stdio-mode-panics branch December 18, 2025 20:50
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 fixes a critical panic in stdio mode that occurs when any tool is called. The panic was introduced after refactoring to context-based dependency injection, where middleware to inject dependencies into the request context was inadvertently omitted.

Key changes:

  • Added middleware in NewMCPServer() to inject ToolDependencies into the request context for all tool handlers
  • Added comprehensive tests for server creation and toolset resolution logic

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/ghmcp/server.go Adds middleware after line 205 to inject deps into context via github.ContextWithDeps(ctx, deps) for all tool handlers
internal/ghmcp/server_test.go Adds new test file with tests for server creation (verifying middleware is configured) and toolset resolution logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v0.26.0+ stdio mode panics: ToolDependencies not found in context

2 participants