mcpserver

package
v0.0.0-...-6deb405 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package mcpserver provides a shared MCP server base that supports Streamable HTTP transport.

Index

Constants

This section is empty.

Variables

View Source
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 NewTool

func NewTool(name, description string, inputSchema map[string]interface{}) *mcp.Tool

NewTool creates an mcp.Tool with a raw JSON Schema input schema.

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

type JSONRPCErr struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

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 New

func New(cfg Config) *Server

New creates a new MCP server with the given config.

func (*Server) AddTool

func (s *Server) AddTool(def ToolDef)

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) MCPServer

func (s *Server) MCPServer() *mcp.Server

MCPServer returns the underlying mcp.Server for advanced usage.

func (*Server) NewHTTPServer

func (s *Server) NewHTTPServer(port int) (*http.Server, error)

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

func (s *Server) Run() error

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

func (s *Server) RunWithArgs(port int) error

RunWithArgs starts the server on the given port (useful for testing).

type ToolDef

type ToolDef struct {
	Tool    *mcp.Tool
	Handler mcp.ToolHandler
}

ToolDef defines a tool to register with the server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL