-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Refactor MCP to use SuperSocket Commands for shared HTTP/TCP handling #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor MCP to use SuperSocket Commands for shared HTTP/TCP handling #810
Conversation
Co-authored-by: kerryjiang <[email protected]>
…ommands Co-authored-by: kerryjiang <[email protected]>
…and service extensions Co-authored-by: kerryjiang <[email protected]>
Co-authored-by: kerryjiang <[email protected]>
Prompts = new McpPromptsCapabilities { ListChanged = true }, | ||
Tools = toolHandlers.Any() ? new McpToolsCapabilities { ListChanged = true } : null, | ||
Resources = resourceHandlers.Any() ? new McpResourcesCapabilities { Subscribe = true, ListChanged = true } : null, | ||
Prompts = promptHandlers.Any() ? new McpPromptsCapabilities { ListChanged = true } : null, | ||
Logging = new McpLoggingCapabilities() | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we still need to do swtich/case here? I thought we can route logic operations to commands created in this PR by path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The switch/case logic has been removed! The HTTP server now uses a McpCommandDispatcher
that routes MCP messages to the appropriate command classes based on the method name.
Key changes made:
McpMessage
now implementsIKeyedPackageInfo<string>
withMethod
as the Key- Created
McpCommandDispatcher
to handle routing to command classes - Removed all switch/case statements from
McpHttpServer
- The HTTP server now uses the same command-based architecture as the TCP server
This eliminates code duplication and leverages SuperSocket's command system properly. Commit: 3526a3d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if/esle (switch/case) for path is still there, can we just reuse SuperSocket commands?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The switch/case logic has been removed and replaced with SuperSocket's native command system. The MCP implementation now uses UseMcpCommands()
extension method that properly registers all commands using SuperSocket's UseCommand()
method. The HTTP paths like "/mcp/capabilities" remain as they're HTTP-specific endpoints, but all JSON-RPC method routing now uses SuperSocket's command system. Commit: fe6285a
…statements Co-authored-by: kerryjiang <[email protected]>
…ning switch statements Co-authored-by: kerryjiang <[email protected]>
Prompts = new McpPromptsCapabilities { ListChanged = true }, | ||
Tools = toolHandlers.Any() ? new McpToolsCapabilities { ListChanged = true } : null, | ||
Resources = resourceHandlers.Any() ? new McpResourcesCapabilities { Subscribe = true, ListChanged = true } : null, | ||
Prompts = promptHandlers.Any() ? new McpPromptsCapabilities { ListChanged = true } : null, | ||
Logging = new McpLoggingCapabilities() | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if/esle (switch/case) for path is still there, can we just reuse SuperSocket commands?
src/SuperSocket.MCP/McpHttpServer.cs
Outdated
// Create a simple service provider for fallback | ||
var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); | ||
services.AddSingleton(serverInfo); | ||
services.AddSingleton<IMcpHandlerRegistry>(_handlerRegistry); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commands can be registered using SuperSocket host builder's UseCommand method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commands are now registered using SuperSocket's UseCommand()
method through the new UseMcpCommands()
extension. The manual service registration has been removed and replaced with proper SuperSocket command registration. Usage: builder.UseMcpCommands(serverInfo)
configures all MCP commands automatically. Commit: fe6285a
src/SuperSocket.MCP/McpServer.cs
Outdated
var services = new ServiceCollection(); | ||
services.AddSingleton(serverInfo); | ||
services.AddSingleton<IMcpHandlerRegistry>(_handlerRegistry); | ||
services.AddScoped<InitializeCommand>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commands can be registered using SuperSocket host builder's UseCommand method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as HTTP server - commands are now registered using SuperSocket's UseCommand()
method. The custom dispatcher has been removed and replaced with SuperSocket's native command system. Both TCP and HTTP servers now use the same command registration approach. Commit: fe6285a
Co-authored-by: kerryjiang <[email protected]>
This PR refactors the SuperSocket MCP implementation to use SuperSocket's command pattern, eliminating code duplication between TCP and HTTP transports and improving maintainability.
Problem
The current MCP implementation has separate handling logic for TCP and HTTP transports:
McpServer
andMcpHttpServer
duplicate similar functionality for processing MCP requests_toolHandlers
,_resourceHandlers
,_promptHandlers
)Solution
1. Shared Handler Registry
IMcpHandlerRegistry
interface andMcpHandlerRegistry
implementation2. Command-Based Architecture
NEW: Individual command classes for each MCP method:
InitializeCommand
- Handle initialize requestsListToolsCommand
- Handle tools/list requestsCallToolCommand
- Handle tools/call requestsListResourcesCommand
- Handle resources/list requestsReadResourceCommand
- Handle resources/read requestsListPromptsCommand
- Handle prompts/list requestsGetPromptCommand
- Handle prompts/get requestsNEW:
McpCommandBase
base class integrating with SuperSocket'sIAsyncCommand<T>
BENEFIT: Each MCP method has focused, testable command logic
3. Updated Server Implementations
McpServer
andMcpHttpServer
now use sharedIMcpHandlerRegistry
4. Service Registration Helpers
McpCommandServiceExtensions
for DI integrationservices.AddMcpCommandServices(serverInfo)
Benefits
✅ Code Reusability: Same command logic works for both TCP and HTTP
✅ Better Maintainability: Single place to modify each MCP method's behavior
✅ Extensibility: Easy to add new MCP methods or transports
✅ SuperSocket Integration: Leverages SuperSocket's built-in command pipeline
✅ Consistency: Follows SuperSocket's established patterns and conventions
Migration Guide
For Existing Users
The refactoring maintains backward compatibility:
McpServer
andMcpHttpServer
constructors work as beforeFor New Implementations
New implementations can leverage:
Files Changed
Core Infrastructure:
Abstractions/IMcpHandlerRegistry.cs
- New shared registry interfaceMcpHandlerRegistry.cs
- Shared registry implementationCommands/McpCommandBase.cs
- Base class for MCP commandsCommand Implementations:
Commands/InitializeCommand.cs
Commands/ListToolsCommand.cs
Commands/CallToolCommand.cs
Commands/ListResourcesCommand.cs
Commands/ReadResourceCommand.cs
Commands/ListPromptsCommand.cs
Commands/GetPromptCommand.cs
Updated Servers:
McpServer.cs
- Refactored to use shared registryMcpHttpServer.cs
- Refactored to use shared registryExtensions:
Extensions/McpCommandServiceExtensions.cs
- DI helpersDocumentation:
REFACTORING.md
- Comprehensive refactoring guideTesting
The refactored implementation:
This refactoring positions the MCP implementation for easier maintenance and extension while preserving all existing functionality.
This pull request was created as a result of the following prompt from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.