mcp

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterDefaultTools

func RegisterDefaultTools(server *MCPServer, executor ToolExecutor)

RegisterDefaultTools registers hawk's standard capabilities as MCP tools. If executor is non-nil, tools that delegate to hawk's tool registry will use it for execution; otherwise those tools return a not-configured error.

func SanitizeToolSchema

func SanitizeToolSchema(schema map[string]interface{}) map[string]interface{}

SanitizeToolSchema prunes an MCP tool's JSON Schema down to the subset that strict OpenAI-compatible function-calling endpoints accept. Many MCP servers publish full JSON Schema documents (with "$schema", "title", "definitions", "additionalProperties", "examples", etc.); several providers (and some local vLLM/SGLang routes) reject a function's parameter schema outright when it carries keys outside the function-calling subset. Qwen-Agent solves this by keeping only {type, properties, required} per tool — hawk does the same.

The function is conservative: it never invents structure. If the input does not look like an object schema (no "type":"object" and no "properties"), it is returned unchanged so non-object tools (rare, but valid) are not corrupted. A nil input yields the canonical empty-object schema so the model still sees a well-formed parameter block.

func SetClientVersion

func SetClientVersion(v string)

SetClientVersion lets main.go propagate the canonical hawk version into this package without creating an import cycle with cmd.

Types

type HTTPServer

type HTTPServer struct {
	Name    string
	URL     string
	Headers map[string]string
	Type    string // "http" or "sse"
	// contains filtered or unexported fields
}

HTTPServer represents an MCP server connected via HTTP or SSE transport.

func ConnectHTTP

func ConnectHTTP(ctx context.Context, name, url string, headers map[string]string) (*HTTPServer, error)

ConnectHTTP connects to an MCP server via HTTP streamable transport.

func ConnectSSE

func ConnectSSE(ctx context.Context, name, url string, headers map[string]string) (*HTTPServer, error)

ConnectSSE connects to an MCP server via Server-Sent Events transport.

func (*HTTPServer) Call

func (s *HTTPServer) Call(ctx context.Context, method string, params interface{}) (json.RawMessage, error)

Call sends a JSON-RPC request and returns the result.

func (*HTTPServer) CallTool

func (s *HTTPServer) CallTool(ctx context.Context, name string, args map[string]interface{}) (string, error)

CallTool invokes a tool on the HTTP/SSE MCP server.

func (*HTTPServer) Close

func (s *HTTPServer) Close() error

Close is a no-op for HTTP/SSE servers (no persistent connection).

func (*HTTPServer) ListTools

func (s *HTTPServer) ListTools(ctx context.Context) ([]Tool, error)

ListTools returns tools from the HTTP/SSE MCP server.

type JSONRPCRequest

type JSONRPCRequest struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      interface{}     `json:"id"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
}

JSON-RPC 2.0 request from a client.

type JSONRPCResponse

type JSONRPCResponse struct {
	JSONRPC string      `json:"jsonrpc"`
	ID      interface{} `json:"id"`
	Result  interface{} `json:"result,omitempty"`
	Error   *RPCError   `json:"error,omitempty"`
}

JSON-RPC 2.0 response to a client.

type MCPServer

type MCPServer struct {
	// contains filtered or unexported fields
}

MCPServer exposes hawk's capabilities as an MCP server that external clients (IDEs, agents, CLI tools) can connect to via JSON-RPC 2.0 over stdio.

func NewMCPServer

func NewMCPServer(info ServerInfo) *MCPServer

NewMCPServer creates a new MCP server with the given identity.

func (*MCPServer) RegisterTool

func (s *MCPServer) RegisterTool(handler MCPToolHandler)

RegisterTool adds a tool to the server. If a tool with the same name already exists, it is replaced.

func (*MCPServer) Serve

func (s *MCPServer) Serve(ctx context.Context, r io.Reader, w io.Writer) error

Serve runs the MCP server reading from r and writing responses to w. This is the testable core of ServeStdio.

func (*MCPServer) ServeStdio

func (s *MCPServer) ServeStdio(ctx context.Context) error

ServeStdio runs the MCP server on stdin/stdout, reading JSON-RPC requests line-by-line and writing responses. It blocks until ctx is cancelled or stdin reaches EOF.

Security: stdio is inherently local — stdin/stdout cannot be reached over the network. If an HTTP/SSE endpoint is added in the future, it MUST default to binding on 127.0.0.1 (localhost) only and accept an explicit flag to bind elsewhere.

type MCPToolHandler

type MCPToolHandler struct {
	Name        string
	Description string
	InputSchema map[string]interface{}
	// Annotations carry behavioral hints (read-only, destructive, …) so a
	// connecting client can self-throttle — e.g. ask the user before a tool
	// that may run shell commands or edit files. Nil means "no hints".
	Annotations *ToolAnnotations
	Handler     func(ctx context.Context, params json.RawMessage) (string, error)
}

MCPToolHandler defines a tool exposed by the MCP server.

type RPCError

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

RPCError is a JSON-RPC 2.0 error object.

type Resource

type Resource struct {
	URI         string `json:"uri"`
	Name        string `json:"name"`
	MimeType    string `json:"mimeType,omitempty"`
	Description string `json:"description,omitempty"`
}

Resource is a resource exposed by an MCP server.

type Server

type Server struct {
	Name    string
	Command string
	Args    []string
	// contains filtered or unexported fields
}

Server represents a connected MCP server.

func Connect

func Connect(ctx context.Context, name, command string, args ...string) (*Server, error)

Connect starts an MCP server process via stdio transport.

func (*Server) CallTool

func (s *Server) CallTool(ctx context.Context, name string, args map[string]interface{}) (string, error)

CallTool invokes a tool on the MCP server.

func (*Server) Close

func (s *Server) Close() error

Close shuts down the MCP server, killing the child process if it doesn't exit within 5 seconds of stdin being closed.

func (*Server) ListResources

func (s *Server) ListResources() ([]Resource, error)

ListResources returns resources available on this MCP server.

func (*Server) ListTools

func (s *Server) ListTools() ([]Tool, error)

ListTools returns tools available on this MCP server.

func (*Server) ReadResource

func (s *Server) ReadResource(uri string) (string, error)

ReadResource reads a resource from this MCP server.

type ServerInfo

type ServerInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

ServerInfo identifies the MCP server to connecting clients.

type Tool

type Tool struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	InputSchema map[string]interface{} `json:"inputSchema"`
}

Tool is a tool exposed by an MCP server.

type ToolAnnotations

type ToolAnnotations struct {
	Title           string `json:"title,omitempty"`
	ReadOnlyHint    *bool  `json:"readOnlyHint,omitempty"`
	DestructiveHint *bool  `json:"destructiveHint,omitempty"`
	IdempotentHint  *bool  `json:"idempotentHint,omitempty"`
	OpenWorldHint   *bool  `json:"openWorldHint,omitempty"`
}

ToolAnnotations are the optional MCP tool behavior hints from the spec. All fields are advisory and pointer-typed so an unset hint is omitted from the wire payload rather than sent as a misleading false.

type ToolExecutor

type ToolExecutor func(ctx context.Context, name string, input json.RawMessage) (string, error)

ToolExecutor is a function that executes a named tool with JSON input. This avoids importing the tool package (which already imports mcp).

type WSServer

type WSServer struct {
	Name    string
	URL     string
	Headers map[string]string
	// contains filtered or unexported fields
}

WSServer represents an MCP server connected via the WebSocket transport. It maintains a single persistent connection over which JSON-RPC requests and responses are multiplexed by request ID, mirroring the stdio transport.

func ConnectWS

func ConnectWS(ctx context.Context, name, rawURL string, headers map[string]string) (*WSServer, error)

ConnectWS connects to an MCP server via the WebSocket transport and performs the JSON-RPC `initialize` handshake. The url scheme may be ws:// or wss://; http:// and https:// are accepted as aliases.

func (*WSServer) Call

func (s *WSServer) Call(ctx context.Context, method string, params interface{}) (json.RawMessage, error)

Call sends a JSON-RPC request over the WebSocket and waits for the response.

func (*WSServer) CallTool

func (s *WSServer) CallTool(ctx context.Context, name string, args map[string]interface{}) (string, error)

CallTool invokes a tool on the WebSocket MCP server.

func (*WSServer) Close

func (s *WSServer) Close() error

Close sends a WebSocket close frame and tears down the connection.

func (*WSServer) ListTools

func (s *WSServer) ListTools(ctx context.Context) ([]Tool, error)

ListTools returns tools from the WebSocket MCP server.

Jump to

Keyboard shortcuts

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