mcp

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package mcp provides a minimal Model Context Protocol server implementation for building AI tool integrations with Claude Desktop and other MCP clients.

Index

Examples

Constants

View Source
const (
	ErrorCodeInternal       = -32603
	ErrorCodeInvalidParams  = -32602
	ErrorCodeInvalidRequest = -32600
	ErrorCodeMethodNotFound = -32601
	ErrorCodeParse          = -32700
)

JSON-RPC 2.0 standard error codes.

Variables

View Source
var (
	ErrMCPInvalidToolParams = errors.New("invalid tool parameters")
	ErrMCPNotInitialized    = errors.New("server not initialized")
	ErrMCPToolNotFound      = errors.New("tool not found")
	ErrMCPTransportClosed   = errors.New("transport closed")
)

MCP-specific errors.

Functions

This section is empty.

Types

type Capabilities

type Capabilities struct {
	Tools *ToolsCapability `json:"tools,omitempty"`
}

Capabilities represents MCP capabilities.

type ContentBlock

type ContentBlock struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

ContentBlock represents a content block in tool results.

func NewTextContent

func NewTextContent(text string) ContentBlock

NewTextContent creates a text content block.

type Implementation

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

Implementation represents server or client implementation info.

type InitializeParams

type InitializeParams struct {
	ProtocolVersion string         `json:"protocolVersion"`
	Capabilities    Capabilities   `json:"capabilities"`
	ClientInfo      Implementation `json:"clientInfo"`
}

InitializeParams represents the parameters for the initialize method.

type InitializeResult

type InitializeResult struct {
	ProtocolVersion string         `json:"protocolVersion"`
	Capabilities    Capabilities   `json:"capabilities"`
	ServerInfo      Implementation `json:"serverInfo"`
}

InitializeResult represents the result of the initialize method.

type InputSchema

type InputSchema struct {
	Type       string              `json:"type"`
	Properties map[string]Property `json:"properties,omitempty"`
	Required   []string            `json:"required,omitempty"`
}

InputSchema represents the JSON Schema for tool input.

func NewObjectSchema

func NewObjectSchema(properties map[string]Property, required []string) InputSchema

NewObjectSchema creates an object input schema.

type Property

type Property struct {
	Type        string `json:"type"`
	Description string `json:"description,omitempty"`
}

Property represents a JSON Schema property.

func NewBooleanProperty

func NewBooleanProperty(description string) Property

NewBooleanProperty creates a boolean property.

func NewNumberProperty

func NewNumberProperty(description string) Property

NewNumberProperty creates a number property.

func NewStringProperty

func NewStringProperty(description string) Property

NewStringProperty creates a string property.

type Request

type Request struct {
	JSONRPC string         `json:"jsonrpc"`
	ID      jsontext.Value `json:"id,omitempty"`
	Method  string         `json:"method"`
	Params  jsontext.Value `json:"params,omitempty"`
}

Request represents a JSON-RPC 2.0 request.

type Response

type Response struct {
	Result  any            `json:"result,omitempty"`
	Error   *ResponseError `json:"error,omitempty"`
	JSONRPC string         `json:"jsonrpc"`
	ID      jsontext.Value `json:"id,omitempty"`
}

Response represents a JSON-RPC 2.0 response.

func NewErrorResponse

func NewErrorResponse(id jsontext.Value, code int, message string) Response

NewErrorResponse creates a JSON-RPC 2.0 error response.

func NewResponse

func NewResponse(id jsontext.Value, result any) Response

NewResponse creates a successful JSON-RPC 2.0 response.

type ResponseError

type ResponseError struct {
	Data    any    `json:"data,omitempty"`
	Message string `json:"message"`
	Code    int    `json:"code"`
}

ResponseError represents a JSON-RPC 2.0 error object.

type Server

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

Server is an MCP server that handles JSON-RPC requests over STDIO.

func NewServer

func NewServer(name, version string) *Server

NewServer creates a new MCP server with the given name and version.

func NewServerWithIO

func NewServerWithIO(name, version string, reader io.Reader, writer io.Writer) *Server

NewServerWithIO creates a new MCP server with custom IO (for testing).

func (*Server) Name added in v0.5.1

func (a *Server) Name() string

Name returns the server name.

func (*Server) RegisterTool

func (a *Server) RegisterTool(tool Tool)

RegisterTool registers a tool with the server.

Example
package main

import (
	"context"
	"fmt"

	"github.com/andygeiss/cloud-native-utils/mcp"
)

func main() {
	server := mcp.NewServer("greeter", "1.0.0")

	server.RegisterTool(mcp.NewTool(
		"greet",
		"Greet someone by name",
		mcp.NewObjectSchema(
			map[string]mcp.Property{"name": mcp.NewStringProperty("who to greet")},
			[]string{"name"},
		),
		func(ctx context.Context, params mcp.ToolsCallParams) (mcp.ToolsCallResult, error) {
			name, _ := params.Arguments["name"].(string)
			return mcp.ToolsCallResult{
				Content: []mcp.ContentBlock{mcp.NewTextContent("Hello, " + name)},
			}, nil
		},
	))

	// server.Serve(ctx) speaks JSON-RPC over stdio. Calling the handler
	// directly is how a test reaches it.
	result, err := server.Tools()[0].Handler(context.Background(), mcp.ToolsCallParams{
		Name:      "greet",
		Arguments: map[string]any{"name": "Alice"},
	})

	fmt.Println(result.Content[0].Text, err)
}
Output:
Hello, Alice <nil>

func (*Server) Serve

func (a *Server) Serve(ctx context.Context) error

Serve starts the server and processes requests until context is canceled.

func (*Server) Tools added in v0.5.1

func (a *Server) Tools() []Tool

Tools returns all registered tools.

func (*Server) Version added in v0.5.1

func (a *Server) Version() string

Version returns the server version.

type Tool

type Tool struct {
	Handler    ToolHandler
	Definition ToolDefinition
}

Tool represents a registered tool with its definition and handler.

func NewTool

func NewTool(name, description string, schema InputSchema, handler ToolHandler) Tool

NewTool creates a new tool with the given definition and handler.

type ToolDefinition

type ToolDefinition struct {
	Name        string      `json:"name"`
	Description string      `json:"description,omitempty"`
	InputSchema InputSchema `json:"inputSchema"`
}

ToolDefinition describes a tool's metadata for tools/list.

type ToolHandler

ToolHandler is a function that handles tool calls. It follows the cloud-native-utils Function pattern.

type ToolsCallParams

type ToolsCallParams struct {
	Arguments map[string]any `json:"arguments,omitempty"`
	Name      string         `json:"name"`
}

ToolsCallParams represents the parameters for tools/call.

type ToolsCallResult

type ToolsCallResult struct {
	Content []ContentBlock `json:"content"`
	IsError bool           `json:"isError,omitzero"`
}

ToolsCallResult represents the result of tools/call.

type ToolsCapability

type ToolsCapability struct {
	ListChanged bool `json:"listChanged,omitzero"`
}

ToolsCapability represents tools capability.

type ToolsListResult

type ToolsListResult struct {
	Tools []ToolDefinition `json:"tools"`
}

ToolsListResult represents the result of tools/list.

Jump to

Keyboard shortcuts

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