Documentation
¶
Overview ¶
Package mcpserver provides a shared MCP server base that supports Streamable HTTP transport.
Index ¶
- Variables
- func NewTool(name, description string, inputSchema map[string]interface{}) *mcp.Tool
- func ParseArgs(logger zerolog.Logger, req *mcp.CallToolRequest) map[string]interface{}
- func ToolError(msg string) *mcp.CallToolResult
- func ToolJSON(data interface{}) *mcp.CallToolResult
- func WrapLegacyHandler(fn func(ctx context.Context, args map[string]interface{}) (interface{}, error)) mcp.ToolHandler
- type AuthConfig
- type Config
- type JSONRPCErr
- type JSONRPCResponse
- type Server
- func (s *Server) AddTool(def ToolDef)
- func (s *Server) AddToolRaw(tool *mcp.Tool, handler mcp.ToolHandler)
- func (s *Server) HandleJSONRPC(ctx context.Context, method string, params json.RawMessage, id any) *JSONRPCResponse
- func (s *Server) MCPServer() *mcp.Server
- func (s *Server) NewHTTPServer(port int) (*http.Server, error)
- func (s *Server) Run() error
- func (s *Server) RunWithArgs(port int) error
- type ToolDef
Constants ¶
This section is empty.
Variables ¶
var DefaultPorts = map[string]int{
"market-data": 8090,
"order-executor": 8091,
"risk-analyzer": 8092,
"technical-indicators": 8093,
"polymarket": 8094,
}
DefaultPorts maps server names to their default HTTP ports.
Functions ¶
func ParseArgs ¶
func ParseArgs(logger zerolog.Logger, req *mcp.CallToolRequest) map[string]interface{}
ParseArgs extracts tool call arguments from a CallToolRequest as a map[string]interface{}. Returns an empty map if arguments are nil or invalid.
The logger parameter should be the server's own logger rather than the global zerolog logger (M5). Pass zerolog.Nop() to silence the warning in tests.
WARNING: Returns empty map on unmarshal failure. Callers must validate required fields independently, as missing arguments will silently appear as zero values rather than triggering an error here.
func ToolError ¶
func ToolError(msg string) *mcp.CallToolResult
ToolError returns a CallToolResult with IsError=true and the given message. Use this to return tool-level errors (as opposed to JSON-RPC errors).
func ToolJSON ¶
func ToolJSON(data interface{}) *mcp.CallToolResult
ToolJSON marshals data to JSON and returns it as a text content result. Returns a ToolError result if marshalling fails.
func WrapLegacyHandler ¶
func WrapLegacyHandler(fn func(ctx context.Context, args map[string]interface{}) (interface{}, error)) mcp.ToolHandler
WrapLegacyHandler wraps a legacy tool handler that takes map[string]interface{} args and returns (interface{}, error) into an mcp.ToolHandler.
Types ¶
type AuthConfig ¶
type AuthConfig struct {
// Token is the shared secret required for access.
// If empty, authentication is disabled.
Token string
// SkipPaths are paths that bypass authentication (e.g., /health).
//
// NOTE (m2): This field is intentionally not populated in production code.
// The outer mux in buildHandler routes /health directly to healthHandler
// before any middleware (including authMiddleware) is applied, so /health
// never reaches the auth layer and does not need to be listed here.
// SkipPaths is preserved to support unit-test scenarios and potential
// future use cases where path-level auth bypass is needed within the
// inner mux.
SkipPaths []string
}
AuthConfig holds configuration for MCP transport authentication.
type Config ¶
type Config struct {
Name string
Version string
// MetricsPort is the port for the Prometheus metrics endpoint.
// If 0, metrics are disabled.
MetricsPort int
Logger zerolog.Logger
}
Config holds server configuration.
type JSONRPCErr ¶
JSONRPCErr is a JSON-RPC 2.0 error object.
type JSONRPCResponse ¶
type JSONRPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID any `json:"id"`
Result interface{} `json:"result,omitempty"`
Error *JSONRPCErr `json:"error,omitempty"`
}
JSONRPCResponse is a JSON-RPC 2.0 response.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server wraps mcp.Server with transport selection and metrics.
func (*Server) AddTool ¶
AddTool registers a tool with the server. The handler is wrapped with metrics instrumentation.
func (*Server) AddToolRaw ¶
func (s *Server) AddToolRaw(tool *mcp.Tool, handler mcp.ToolHandler)
AddToolRaw registers a tool using raw mcp.Tool and mcp.ToolHandler directly.
func (*Server) HandleJSONRPC ¶
func (s *Server) HandleJSONRPC(ctx context.Context, method string, params json.RawMessage, id any) *JSONRPCResponse
HandleJSONRPC dispatches a JSON-RPC request to the appropriate handler. Exported for testing compatibility.
func (*Server) NewHTTPServer ¶
NewHTTPServer creates an HTTP server with the MCP handler configured but does not start it. The caller is responsible for starting and shutting it down.
func (*Server) Run ¶
Run parses CLI flags and starts the server with the selected transport. It uses a scoped FlagSet (not flag.CommandLine) to avoid global state conflicts when multiple Server instances exist in the same process.
func (*Server) RunWithArgs ¶
RunWithArgs starts the server on the given port (useful for testing).