opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Added `mcp` CLI command

Open ndraiman opened this issue 6 months ago • 10 comments

Summary

  • Add comprehensive MCP (Model Context Protocol) server management CLI commands
  • Implement mcp add-json command with intelligent transport type inference
  • Support both stdio and SSE transport configurations with automatic validation

Changes

New CLI Commands

  • mcp add - Add MCP servers with stdio/SSE transport options
  • mcp remove - Remove configured MCP servers
  • mcp list - List all configured MCP servers
  • mcp get - Get details about a specific MCP server
  • mcp add-json - Add MCP servers using JSON configuration with automatic type inference

Key Features

  • Smart Type Inference: add-json command automatically determines local/remote type based on JSON structure
  • Transport Format Transformation: Converts flexible JSON formats to match internal schema requirements
  • Dual Scope Support: Configure servers at user or project level
  • Transport Validation: Enforces stdio/SSE constraints and validates inputs
  • Environment & Headers: Support for environment variables (stdio) and HTTP headers (SSE)

Technical Implementation

  • JSON schema validation with Zod
  • Automatic command/args merging for stdio transport
  • Environment variable mapping (env → environment)
  • Type field removal and inference from transport properties
  • Comprehensive error handling and user feedback

Test Coverage

  • Full test suite covering all commands and edge cases
  • Validation testing for transport constraints
  • Both user and project scope configuration testing
  • Error handling verification

🤖 Generated with https://claude.ai/code

Co-Authored-By: Claude

ndraiman avatar Jun 24 '25 20:06 ndraiman

wow i was gonna work on this - i'll take a look ty!

thdxr avatar Jun 25 '25 01:06 thdxr

Thank you. 🙏🏻

I didn't implement the full list of sub-commands like in claude mcp because I didn't know how far you want to make it the same.

Commands I didn't implement:

  • serve
  • add-from-claude-desktop
  • reset-project-choices

Also Claude has "local" scope for mcp.

ndraiman avatar Jun 25 '25 04:06 ndraiman

This sounds awesome, thanks a lot @ndraiman!

aspiers avatar Jun 28 '25 15:06 aspiers

  1. Updated to include support for enable / disable commands
  2. Fixed mcp.test file to use a mock local config instead of overriding the existing opencode.json in project root.

ndraiman avatar Jun 29 '25 03:06 ndraiman

Hey, nice work! Do you plan to integrate /mcp commands (to TUI) next? I would love to help

3dyuval avatar Jul 06 '25 10:07 3dyuval

Looking at the code architecture now, I see that I should have:

  1. Extracted logic to functions
  2. Exposed logic as server endpoints
  3. TUI will be able to control MCP configs via Client sdk

Hopefully I'll be able to this later today.

ndraiman avatar Jul 09 '25 04:07 ndraiman

Yes. I'd love to learn how the model, view, and the functional logic are clearly seperated in the solution.

3dyuval avatar Jul 09 '25 10:07 3dyuval

@ndraiman i don't think we need to support this in the tui, probably dont want to have to maintain all that; cli only is fine!

adamdotdevin avatar Jul 09 '25 12:07 adamdotdevin

Any /mcp slash command in the tui would be a big welcome to figure out the state of the configured mcp servers

Ramblurr avatar Aug 16 '25 08:08 Ramblurr

Hello, what current status of this feature ?

ZGltYQ avatar Oct 20 '25 14:10 ZGltYQ