Skip to content

feat: add Model Context Protocol (MCP) server with HTTP transport and authentication#1468

Open
YuZhangLarry wants to merge 1 commit into
apache:aifrom
YuZhangLarry:mcp-to-ai
Open

feat: add Model Context Protocol (MCP) server with HTTP transport and authentication#1468
YuZhangLarry wants to merge 1 commit into
apache:aifrom
YuZhangLarry:mcp-to-ai

Conversation

@YuZhangLarry
Copy link
Copy Markdown

Summary

Implement Model Context Protocol (MCP) server for Dubbo Admin to enable AI integration through standardized tool interfaces. This allows AI assistants (like Claude) to directly interact with Dubbo resources.

Architecture

Core Components

  • Server (pkg/mcp/core/): MCP protocol implementation with JSON-RPC 2.0
  • Registry (pkg/mcp/registry/): Tool registration and management
  • Tools (pkg/mcp/tools/): 11 tools for Dubbo resource operations
  • Transport (pkg/mcp/transport/): Stdio and HTTP transport layers
  • Types (pkg/mcp/types/): Request/response schemas and validation

Transport Layers

  • Stdio: For local Claude Desktop integration via stdin/stdout
  • HTTP: For remote connections with CORS and SSE support
  • Authentication: Optional Bearer Token for HTTP endpoint

Tool Categories

  • Cluster: get_cluster_info, global_search
  • Service: search_services, get_service_detail, get_service_instances
  • Instance: search_instances, get_instance_detail, get_instance_metrics
  • Application: get_application_detail, get_application_instances, get_application_services

Configuration

mcp:
  enabled: true
  path: /api/mcp
  apiKey: "your-secret-key"  # Optional
**Please check any characteristics that apply to this pull request.**

…t and authentication

## Summary
Implement Model Context Protocol (MCP) server for Dubbo Admin to enable AI integration through standardized tool interfaces.

## Key Features
- **Modular Architecture**: Core components (server, registry, tools, transport, types)
- **Comprehensive Tool Support**: 11 tools covering cluster info, service discovery, instance management, metrics, and application details
- **Dual Transport Support**:
  - Stdio transport for local Claude Desktop integration
  - HTTP transport for remote connections with JSON-RPC 2.0
- **Security**: Optional Bearer Token authentication for HTTP endpoint

## Configuration

## Test plan
- [x] Unit tests for core components
- [x] Integration tests
- [x] Manual testing with Claude Desktop

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

@robocanic robocanic left a comment

Choose a reason for hiding this comment

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

Great work! 有两个大面上需要修正的地方:

  1. 注释需要用英文
  2. 包导入需要按照这个规范来:https://github.com/apache/dubbo-admin/wiki/%E6%96%B0%E7%89%88Dubbo-Admin%E5%BC%80%E5%8F%91%E8%80%85%E6%8C%87%E5%8D%97#goland-import%E9%85%8D%E7%BD%AE

Copy link
Copy Markdown
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 introduces a Model Context Protocol (MCP) server implementation for Dubbo Admin, exposing Dubbo resource operations as standardized JSON-RPC tools over both stdio (local) and HTTP (remote) transports, with optional Bearer-token authentication for the HTTP endpoint.

Changes:

  • Added MCP core server, tool schemas/validation, and tool registry abstractions.
  • Implemented stdio and HTTP transports (including an SSE transport variant) plus related tests.
  • Added built-in MCP tools for cluster info, global search, service discovery, and detail queries; wired MCP endpoint into the console web server and extended app config with MCP settings.

Reviewed changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 17 comments.

Show a summary per file
File Description
pkg/mcp/types/validation.go Adds required-arg validation helpers used by tool invocation.
pkg/mcp/types/tool.go Defines tool schema/handler/result types for MCP tools.
pkg/mcp/transport/stdio/stdio.go Implements newline-delimited JSON-RPC over stdin/stdout.
pkg/mcp/transport/stdio/stdio_test.go Unit tests for stdio transport request/response handling.
pkg/mcp/transport/stdio/stdio_integration_test.go End-to-end tests over in-memory pipes for stdio transport.
pkg/mcp/transport/http/sse.go Adds an SSE transport implementation for streaming-like responses/events.
pkg/mcp/transport/http/http.go Implements an HTTP server wrapper for MCP handling.
pkg/mcp/transport/http/http_test.go Tests HTTP handler/transport behavior, CORS, concurrency, and SSE.
pkg/mcp/transport/http/handler.go HTTP handler for JSON-RPC requests with basic CORS support.
pkg/mcp/tools/utils.go Shared argument parsing, pagination helpers, and JSON formatting utilities.
pkg/mcp/tools/tools_test.go Unit tests for tool utilities, registration, and validation helpers.
pkg/mcp/tools/service_discovery.go Implements service search and service detail MCP tools.
pkg/mcp/tools/resource_search.go Implements global search MCP tool with multiple search modes.
pkg/mcp/tools/metrics.go Implements cluster metrics/info tool using counters.
pkg/mcp/tools/live_test.go Provides optional “live” testing harness for MCP tools against real deps.
pkg/mcp/tools/integration_test.go Integration-style tests for MCP server + registry + transports.
pkg/mcp/tools/detail_tools.go Adds instance/app/service detail and instance search tools.
pkg/mcp/registry/registry.go Implements the MCP tool registry and registrar mechanism.
pkg/mcp/registry/registry_test.go Tests registry CRUD and registrar integration.
pkg/mcp/mcp.go Public package facade: re-exports core types and default tool registration.
pkg/mcp/core/types.go Defines JSON-RPC and MCP result types returned by the server.
pkg/mcp/core/tool.go Aliases types into core and re-exports constructors/validators.
pkg/mcp/core/server.go MCP server logic for initialize/tools/list/tools/call and HTTP handler glue.
pkg/mcp/core/server_test.go Tests server construction, builder, and registry operations.
pkg/mcp/core/constants.go Defines MCP/JSON-RPC constants and error codes.
pkg/mcp/core/builder.go Adds a builder for constructing MCP servers with optional console context.
pkg/mcp/component.go Adds MCP as a runtime component (currently placeholder integration).
pkg/core/bootstrap/init.go Registers MCP component in bootstrap imports.
pkg/console/component.go Registers MCP endpoint route and adds optional Bearer-token auth for it.
pkg/config/app/admin.go Extends app config (incl. MCP config) and refactors config fields/validation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/mcp/tools/metrics.go
Comment on lines +56 to +60
// collectClusterInfo 收集集群信息
func collectClusterInfo(ctx consolectx.Context, mesh string) map[string]any {
counterMgr := ctx.CounterManager()
if counterMgr == nil {
return map[string]any{
Comment thread pkg/config/app/admin.go
Comment on lines 35 to 39
type AdminConfig struct {
config.BaseConfig
// Mode in which dubbo admin is running. Available values are: "test", "global", "zone"
Mode mode.Mode `json:"mode" envconfig:"DUBBO_MODE"`
// Log configuration
Log *log.Config `json:"log" yaml:"log"`
// Diagnostics configuration
Comment thread pkg/config/app/admin.go
Comment on lines +130 to +134
func (c AdminConfig) Validate() error {
if c.Log == nil {
c.Log = log.DefaultLogConfig()
} else if err := c.Log.Validate(); err != nil {
return bizerror.Wrap(err, bizerror.ConfigError, "log config validation failed")
Comment on lines +167 to +172
return map[string]any{
"keyword": keyword,
"pageSize": pageSize,
"pageNumber": pageNumber,
"applications": apps,
"totalCount": len(apps),
// GetServiceDetail 获取服务详情
func GetServiceDetail(ctx consolectx.Context, args map[string]any) (*types.ToolResult, error) {
helper := NewArgsHelper(args)
serviceName := helper.GetString("serviceName", "")
Comment on lines +52 to +55
Handler: func(ctx interface{}, args map[string]any) (*core.ToolResult, error) {
msg, _ := args["message"].(string)
return core.NewTextResult("Echo: " + msg), nil
},
Comment on lines +260 to +265
req := httptest.NewRequest("GET", "/sse", nil)
w := httptest.NewRecorder()

// SSE需要flusher支持,httptest.NewRecorder实现了Flusher接口
sseTransport.HandleSSE(w, req)

Comment thread pkg/console/component.go
Comment on lines +235 to +239
}
token := authHeader[7:]
if token != c.mcpAPIKey {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid API key"})
ctx.Abort()
Comment thread pkg/mcp/component.go
Comment on lines +139 to +143
handler := http.NewHandler(server)

// 注册MCP端点(不需要认证)
// 注意:这只是示例代码,实际注册应该在console组件中完成
_ = handler // 避免未使用警告
Comment thread pkg/mcp/component.go
Comment on lines +117 to +121
// 通过runtime获取gin.Engine
// 这里需要一种方式来访问console组件的gin.Engine
// 暂时使用全局注册方式
RegisterMCPRoutes(c.server, c.cfg.Path)

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.

3 participants