Skip to content

feat: add MCP servers to agent mode - #379

Open
km-anthropic wants to merge 5 commits into
mainfrom
km/add-mcp-to-agent
Open

km-anthropic wants to merge 5 commits into
mainfrom
km/add-mcp-to-agent

Conversation

@km-anthropic

Copy link
Copy Markdown
Collaborator

Summary

  • Enables full MCP server capabilities in agent mode (previously had empty MCP config)
  • Updates prepareMcpConfig to handle both entity contexts (PRs/issues) and automation contexts (workflow_dispatch/schedule)
  • Allows bot actors to access Claude's full tool capabilities via agent mode

Context

Agent mode was designed for automation events but had minimal MCP configuration. This limited its usefulness for scenarios where automated workflows need access to GitHub tools (comments, file operations, CI tools, etc).

Changes

  1. Updated agent mode to call prepareMcpConfig instead of using an empty MCP configuration
  2. Modified prepareMcpConfig to accept both ParsedGitHubContext and AutomationContext types
  3. Added type guards ('isPR' in context) to safely handle context-specific properties

Benefits

  • Bot actors can now use workflow_dispatch to trigger Claude with full MCP capabilities
  • Users can control which tools are available via the allowed_tools parameter
  • Consistent MCP configuration across all modes
  • Enables more powerful automation workflows

Testing

  • All existing tests pass
  • Type checking passes with the updated context types

@claude

claude Bot commented Aug 1, 2025 •

Copy link
Copy Markdown
Contributor

Claude finished @km-anthropic's task —— View job


Code Review: feat: add MCP servers to agent mode

  • Analyze the core changes to agent mode MCP configuration
  • Review type safety improvements and context handling
  • Examine the prepareMcpConfig function changes
  • Check documentation consistency in README.md
  • Review new review mode implementation
  • Analyze test updates and coverage
  • Provide comprehensive feedback and recommendations

Summary

This PR successfully enables full MCP server capabilities in agent mode by integrating the existing prepareMcpConfig function instead of using an empty configuration. The changes are well-implemented with proper type safety and comprehensive test coverage.

Positive Aspects

1. Architecture & Design ✅

  • Clean Integration: Reuses existing prepareMcpConfig function rather than duplicating logic
  • Consistent API: Agent mode now shares the same MCP setup as other modes while maintaining its automation-focused identity
  • Type Safety: Proper use of discriminated unions and type guards ('isPR' in context) to handle different context types safely

2. Code Quality ✅

  • Type Guards: Smart use of 'isPR' in context to safely access context-specific properties (line 118 in src/mcp/install-mcp-server.ts)
  • Parameter Handling: Clean parameter passing with all necessary context fields
  • Error Handling: Maintains existing error handling patterns

3. Testing ✅

  • Comprehensive Coverage: Added AutomationContext support with proper mock implementations
  • Mode Registry Tests: Tests validate that agent mode correctly handles automation events
  • Test Infrastructure: New createMockAutomationContext helper for testing automation scenarios

4. Documentation ✅

  • README Updates: Agent mode section now accurately reflects full MCP capabilities
  • Clear Use Cases: Documentation clearly explains when to use agent mode vs other modes
  • Experimental Review Mode: Well-documented new mode with appropriate warnings

Technical Analysis

Core Changes Analysis

Before: Agent mode used minimal MCP config with empty mcpServers: {}
After: Agent mode now calls prepareMcpConfig with full context support

Key improvements in src/modes/agent/index.ts:73-83:

const mcpConfig = await prepareMcpConfig({
  githubToken,
  owner: context.repository.owner,
  repo: context.repository.repo,
  branch: "", // Agent mode doesn't use branches
  baseBranch: "",
  additionalMcpConfig,
  claudeCommentId: undefined, // Agent mode doesn't track comments
  allowedTools: [...baseTools, ...context.inputs.allowedTools],
  context, // Now passes full context
});

Type Safety Improvements

The prepareMcpConfig function now accepts both ParsedGitHubContext and AutomationContext via the unified GitHubContext type, with safe property access:

// src/mcp/install-mcp-server.ts:118
if ('isPR' in context && context.isPR && hasActionsReadPermission) {

This pattern safely handles the fact that AutomationContext doesn't have isPR or entityNumber properties.

Minor Suggestions

1. Branch Handling Clarity

The empty strings for branch and baseBranch in agent mode could be more explicit:

// Consider adding constants for clarity
const AGENT_MODE_BRANCH = ""; // Agent mode doesn't use branches
const AGENT_MODE_BASE_BRANCH = ""; // Agent mode doesn't track base branches

2. Comment Consistency

Consider adding a comment explaining why claudeCommentId is undefined in agent mode:

claudeCommentId: undefined, // Agent mode doesn't create tracking comments

3. Type Documentation

The PrepareConfigParams type could benefit from JSDoc comments explaining which parameters are optional for agent mode contexts.

Questions for Consideration

  1. Tool Filtering: Should agent mode have different default tool restrictions than other modes, given its automation focus?

  2. MCP Server Selection: Are there specific MCP servers that should be excluded/included by default in agent mode?

  3. Error Handling: Should agent mode handle MCP configuration failures differently since it's often used in automated contexts?

Conclusion

This is a well-executed PR that successfully addresses the stated goal. The implementation:

  • ✅ Maintains type safety across different context types
  • ✅ Reuses existing, tested infrastructure
  • ✅ Includes comprehensive test coverage
  • ✅ Updates documentation appropriately
  • ✅ Follows existing code patterns and conventions

The changes enable powerful automation scenarios where bot actors can leverage Claude's full MCP toolset via workflow_dispatch, while maintaining clean separation between mode concerns.

Recommendation: APPROVE - This PR is ready for merge. The implementation is solid, well-tested, and addresses a legitimate need for enhanced automation capabilities.

@km-anthropic
km-anthropic marked this pull request as draft August 1, 2025 21:15
- Update agent mode to use prepareMcpConfig instead of empty config
- Update prepareMcpConfig to accept both ParsedGitHubContext and AutomationContext
- Add type guards for context-specific properties (isPR, entityNumber)
- This enables bot actors to use full MCP capabilities via workflow_dispatch
Remove the generatePrompt method from agent mode since it's not part of the Mode interface in the current main branch. Also remove unused PreparedContext import.
@km-anthropic
km-anthropic marked this pull request as ready for review August 1, 2025 22:04
@km-anthropic
km-anthropic requested a review from ashwin-ant August 1, 2025 22:04
Comment thread src/mcp/install-mcp-server.ts Resolved
Resolved conflict by keeping both MCP server changes and generatePrompt method
Address review feedback by using isEntityContext() type guard instead of
checking 'isPR' in context manually. This provides better type safety
and uses the existing type guard infrastructure.
@km-anthropic
km-anthropic marked this pull request as draft August 1, 2025 22:50
@km-anthropic
km-anthropic marked this pull request as ready for review August 1, 2025 23:05
@km-anthropic
km-anthropic requested a review from ashwin-ant August 1, 2025 23:05
Comment thread src/mcp/install-mcp-server.ts Resolved
REPO_OWNER: owner,
REPO_NAME: repo,
PR_NUMBER: context.entityNumber?.toString() || "",
PR_NUMBER: isEntityContext(context)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Unfortunately have to deal with PR Numbers (even though Agent Mode doesn't work with PR Events yet) because of the github actions MCP (that's one of the default ones)

This branch has not been deployed

No deployments
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.

2 participants