mcp

package module
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: MIT Imports: 14 Imported by: 7

README

MCP Server Library for Go

A Go library for building Model Context Protocol (MCP) servers with a clean, fluent API.

Features

  • Simple API: Fluent interface for defining tools and parameters
  • Type Safety: Strongly typed parameter access with automatic conversion
  • Rich Responses: Support for text, image, audio, resource, and structured content
  • Thread Safe: Concurrent request handling with mutex protection
  • Remote Servers: Connect to and proxy remote MCP servers with authentication
  • Unified Interface: Combine local and remote tools in a single server
  • Searchable Tools: Reduce context window usage with on-demand tool discovery
  • Dynamic Tool Providers: Load tools from external sources (scripts, databases, APIs)

Installation

go get github.com/paularlott/mcp

Quick Start

package main

import (
    "context"
    "fmt"
    "log"
    "net/http"

    "github.com/paularlott/mcp"
)

func main() {
    // Create server
    server := mcp.NewServer("my-server", "1.0.0")

    // Register a tool
    server.RegisterTool(
        mcp.NewTool("greet", "Greet someone",
            mcp.String("name", "Name to greet", mcp.Required()),
            mcp.String("greeting", "Custom greeting"),
        ),
        func(ctx context.Context, req *mcp.ToolRequest) (*mcp.ToolResponse, error) {
            name, _ := req.String("name")
            greeting := req.StringOr("greeting", "Hello")
            return mcp.NewToolResponseText(fmt.Sprintf("%s, %s!", greeting, name)), nil
        },
    )

    // Start server
    http.HandleFunc("/mcp", server.HandleRequest)
    log.Fatal(http.ListenAndServe(":8000", nil))
}

Tool Definition

Parameter Types

The library provides a clean, declarative API for defining tools:

// Basic types
mcp.String(name, description, options...)
mcp.Number(name, description, options...)
mcp.Boolean(name, description, options...)

// Array types
mcp.StringArray(name, description, options...)
mcp.NumberArray(name, description, options...)

// Object types
mcp.Object(name, description, properties...)
mcp.ObjectArray(name, description, properties...)

// Options
mcp.Required()  // Makes parameter required
mcp.Output(...) // Defines structured output
Object Support

The library provides comprehensive support for objects and arrays of objects with a clean, declarative syntax:

Basic Object Parameters

Define structured object parameters with typed properties:

server.RegisterTool(
    mcp.NewTool("create_user", "Create a new user",
        mcp.Object("user", "User information",
            mcp.String("name", "User's full name", mcp.Required()),
            mcp.String("email", "User's email address", mcp.Required()),
            mcp.Number("age", "User's age"),
            mcp.Boolean("active", "Whether user is active"),
            mcp.StringArray("tags", "User tags"),
            mcp.Required(),
        ),
        mcp.Output(
            mcp.Object("result", "Creation result",
                mcp.String("id", "User ID"),
                mcp.StringArray("permissions", "User permissions"),
            ),
        ),
    ),
    handleCreateUser,
)

Extract object parameters in your handler:

func handleCreateUser(ctx context.Context, req *mcp.ToolRequest) (*mcp.ToolResponse, error) {
    // Get the entire object
    user, err := req.Object("user")
    if err != nil {
        return nil, err
    }

    // Extract specific properties with type safety
    name, err := req.GetObjectStringProperty("user", "name")
    if err != nil {
        return nil, err
    }

    email, err := req.GetObjectStringProperty("user", "email")
    if err != nil {
        return nil, err
    }

    // Optional properties with manual checking
    age := 0
    if ageVal, exists := user["age"]; exists {
        if ageFloat, ok := ageVal.(float64); ok {
            age = int(ageFloat)
        }
    }

    return mcp.NewToolResponseText(fmt.Sprintf("Created user: %s (%s)", name, email)), nil
}
Array of Objects

Define and handle arrays of objects:

server.RegisterTool(
    mcp.NewTool("process_orders", "Process multiple orders",
        mcp.ObjectArray("orders", "List of orders to process",
            mcp.String("id", "Order ID", mcp.Required()),
            mcp.Number("amount", "Order amount", mcp.Required()),
            mcp.String("currency", "Currency code"),
            mcp.Required(),
        ),
        mcp.Output(
            mcp.StringArray("processed_ids", "List of processed order IDs"),
            mcp.NumberArray("totals", "List of order totals"),
        ),
    ),
    handleProcessOrders,
)

func handleProcessOrders(ctx context.Context, req *mcp.ToolRequest) (*mcp.ToolResponse, error) {
    orders, err := req.ObjectSlice("orders")
    if err != nil {
        return nil, err
    }

    for i, order := range orders {
        id, ok := order["id"].(string)
        if !ok {
            return nil, fmt.Errorf("order %d missing or invalid id", i)
        }

        amount, ok := order["amount"].(float64)
        if !ok {
            return nil, fmt.Errorf("order %d missing or invalid amount", i)
        }

        // Process order...
    }

    return mcp.NewToolResponseText("Orders processed"), nil
}
Nested Objects

Objects can contain other objects and arrays:

server.RegisterTool(
    mcp.NewTool("create_order", "Create an order with customer info",
        mcp.Object("order", "Order information",
            mcp.String("id", "Order ID", mcp.Required()),
            mcp.Number("total", "Order total", mcp.Required()),
            mcp.Object("customer", "Customer information",
                mcp.String("name", "Customer name", mcp.Required()),
                mcp.String("email", "Customer email"),
                mcp.Required(),
            ),
            mcp.ObjectArray("items", "Order items",
                mcp.String("sku", "Item SKU", mcp.Required()),
                mcp.Number("quantity", "Item quantity", mcp.Required()),
            ),
            mcp.Required(),
        ),
    ),
    handleCreateOrder,
)
Generic Objects

For cases where you need to accept arbitrary object structures:

server.RegisterTool(
    mcp.NewTool("configure", "Configure with arbitrary settings",
        mcp.Object("config", "Configuration object", mcp.Required()),
    ),
    handleConfigure,
)

Generic objects allow any properties and generate a schema with "additionalProperties": true.

API Reference

Parameter Functions:

  • String(name, description, options...) - String parameter
  • Number(name, description, options...) - Number parameter
  • Boolean(name, description, options...) - Boolean parameter
  • StringArray(name, description, options...) - Array of strings
  • NumberArray(name, description, options...) - Array of numbers
  • Object(name, description, properties...) - Object with properties (use mcp.Required() to make required)
  • ObjectArray(name, description, properties...) - Array of objects (use mcp.Required() to make required)

Options:

  • Required() - Makes any parameter required
  • Output(parameters...) - Defines structured output schema

ToolRequest Methods:

  • Object(name) - Extract an object parameter as map[string]interface{}
  • ObjectOr(name, default) - Extract an object parameter with default
  • ObjectSlice(name) - Extract an array of objects as []map[string]interface{}
  • ObjectSliceOr(name, default) - Extract an array of objects with default
  • GetObjectProperty(objectName, propertyName) - Get a property from an object
  • GetObjectStringProperty(objectName, propertyName) - Get a string property with type safety
  • GetObjectIntProperty(objectName, propertyName) - Get an int property with type safety
  • GetObjectBoolProperty(objectName, propertyName) - Get a bool property with type safety
Generated JSON Schema

The library generates proper JSON Schema for object parameters:

{
  "type": "object",
  "properties": {
    "user": {
      "type": "object",
      "description": "User information",
      "properties": {
        "name": {
          "type": "string",
          "description": "User's full name"
        },
        "email": {
          "type": "string",
          "description": "User's email address"
        },
        "age": {
          "type": "number",
          "description": "User's age"
        }
      },
      "required": ["name", "email"],
      "additionalProperties": false
    }
  },
  "required": ["user"],
  "additionalProperties": false
}
Complete Example
tool := mcp.NewTool("example", "Comprehensive example tool",
    // Input parameters
    mcp.String("name", "User name", mcp.Required()),
    mcp.Number("age", "User age"),
    mcp.StringArray("tags", "User tags"),
    mcp.Object("address", "User address",
        mcp.String("street", "Street address", mcp.Required()),
        mcp.String("city", "City", mcp.Required()),
    ),

    // Structured output
    mcp.Output(
        mcp.String("user_id", "Created user ID"),
        mcp.StringArray("permissions", "User permissions"),
        mcp.Object("profile", "User profile",
            mcp.String("display_name", "Display name"),
            mcp.Boolean("verified", "Is verified"),
        ),
    ),
)```

## Request Handling

### Type-Safe Parameter Access

```go
func handler(ctx context.Context, req *mcp.ToolRequest) (*mcp.ToolResponse, error) {
    // Required parameters (returns error if missing/wrong type)
    name, err := req.String("name")
    count, err := req.Int("count")
    enabled, err := req.Bool("enabled")
    price, err := req.Float("price")

    // Optional parameters with defaults
    greeting := req.StringOr("greeting", "Hello")
    limit := req.IntOr("limit", 10)
    debug := req.BoolOr("debug", false)
    rate := req.FloatOr("rate", 1.0)

    // Array parameters
    tags, err := req.StringSlice("tags")
    numbers, err := req.IntSlice("numbers")

    // Object parameters
    user, err := req.Object("user")
    orders, err := req.ObjectSlice("orders")

    // Extract object properties with type safety
    userName, err := req.GetObjectStringProperty("user", "name")
    userAge, err := req.GetObjectIntProperty("user", "age")

    return mcp.NewToolResponseText("Success"), nil
}

Response Types

Text Response
return mcp.NewToolResponseText("Hello, world!")
Image Response (auto base64 encoded)
imageBytes, _ := os.ReadFile("image.png")
return mcp.NewToolResponseImage(imageBytes, "image/png")
Audio Response (auto base64 encoded)
audioBytes, _ := os.ReadFile("audio.wav")
return mcp.NewToolResponseAudio(audioBytes, "audio/wav")
Resource Response
return mcp.NewToolResponseResource("file://path", "content", "text/plain")
return mcp.NewToolResponseResourceLink("https://example.com", "View details")
Structured Response
data := map[string]interface{}{
    "status": "success",
    "count": 42,
}
return mcp.NewToolResponseStructured(data)
Multi-Content Response
response1 := mcp.NewToolResponseText("Results:")
response2 := mcp.NewToolResponseImage(imageBytes, "image/png")
return mcp.NewToolResponseMulti(response1, response2)
Error Responses
// Invalid parameter error
if name == "" {
    return nil, mcp.NewToolErrorInvalidParams("name parameter is required")
}

// Internal server error
if err := someOperation(); err != nil {
    return nil, mcp.NewToolErrorInternal("failed to process request")
}

// Custom error with specific code
return nil, mcp.NewToolError(-32000, "Custom server error", map[string]interface{}{
    "details": "Additional error information",
})

Server Configuration

server := mcp.NewServer("server-name", "1.0.0")

// Register multiple tools
server.RegisterTool(tool1, handler1)
server.RegisterTool(tool2, handler2)

// Handle MCP requests
http.HandleFunc("/mcp", server.HandleRequest)

Protocol Support

The library supports MCP protocol versions:

  • 2024-11-05 (minimum)
  • 2025-03-26
  • 2025-06-18 (latest)

Thread Safety

The server is thread-safe and can handle concurrent requests. Tool registration and execution are protected by mutexes.

Remote Servers and Clients

MCP Client

Connect to remote MCP servers:

// Bearer token authentication
auth := mcp.NewBearerTokenAuth("your-token")
client := mcp.NewClient("https://api.example.com/mcp", auth)

// OAuth2 authentication
oauth := mcp.NewOAuth2Auth("client-id", "client-secret", "https://auth.example.com/token", []string{"mcp:read"})
client := mcp.NewClient("https://api.example.com/mcp", oauth)

// Use client
tools, err := client.ListTools(ctx)
result, err := client.CallTool(ctx, "tool-name", args)
Unified Server with Remote Tools

Register remote servers directly with your local server for a unified tool interface:

// Create server
server := mcp.NewServer("my-server", "1.0.0")

// Register local tools (as usual)
server.RegisterTool(
    mcp.NewTool("local-greet", "Local greeting").
        AddParam("name", mcp.String, "Name to greet", true),
    func(ctx context.Context, req *mcp.ToolRequest) (*mcp.ToolResponse, error) {
        name, _ := req.String("name")
        return mcp.NewToolResponseText(fmt.Sprintf("Hello, %s!", name)), nil
    },
)

// Register remote servers with namespaces
bearerAuth := mcp.NewBearerTokenAuth("ai-tools-token")
server.RegisterRemoteServer("https://ai.example.com/mcp", "ai", bearerAuth)

oauth2Auth := mcp.NewOAuth2Auth("client-id", "client-secret", "https://auth.example.com/token", []string{"mcp:read"})
server.RegisterRemoteServer("https://data.example.com/mcp", "data", oauth2Auth)

// ListTools returns all tools (local + remote with namespaces)
tools := server.ListTools() // Returns: ["local-greet", "ai/generate-text", "data/query", ...]

// CallTool with intelligent routing
result, err := server.CallTool(ctx, "local-greet", args)       // Calls local tool
result, err := server.CallTool(ctx, "ai/generate-text", args)  // Calls remote AI tool
result, err := server.CallTool(ctx, "unknown-tool", args)      // Returns ErrUnknownTool

// Serve unified interface as HTTP endpoint
http.HandleFunc("/mcp", server.HandleRequest)
Tool Resolution
  • Namespaced calls (namespace/tool-name): Route directly to the specified remote server
  • Non-namespaced calls: Try local tools first, then fast lookup for remote tools
  • Caching: Remote tool lists are cached and can be refreshed with RefreshTools()
  • Error handling: Failed remote servers are skipped gracefully during registration
Authentication

Bearer Token:

auth := mcp.NewBearerTokenAuth("your-token")

OAuth2 with Client Credentials:

auth := mcp.NewOAuth2Auth(
    "client-id",
    "client-secret",
    "https://auth.example.com/token",
    []string{"mcp:read", "mcp:execute"},
)

Error Handling

The library provides structured error handling:

// Tool-specific errors
if name == "" {
    return nil, mcp.NewToolErrorInvalidParams("name parameter is required")
}

// Internal errors
if err := someOperation(); err != nil {
    return nil, mcp.NewToolErrorInternal("operation failed")
}

// Custom errors
return nil, mcp.NewToolError(-32000, "Custom error", map[string]interface{}{
    "details": "Additional information",
})

API Reference

Server Methods
  • NewServer(name, version string) *Server - Create a new server
  • RegisterTool(tool *ToolBuilder, handler ToolHandler) - Register a local tool
  • RegisterRemoteServer(url, namespace string, auth AuthProvider) error - Register a remote server
  • ListTools() []MCPTool - Get all tools (local + remote)
  • CallTool(ctx context.Context, name string, args map[string]interface{}) (*ToolResponse, error) - Execute a tool
  • RefreshTools() error - Refresh remote tool cache
  • HandleRequest(w http.ResponseWriter, r *http.Request) - HTTP handler for MCP requests
Client Methods
  • NewClient(baseURL string, auth AuthProvider) *Client - Create a new client
  • Initialize(ctx context.Context) error - Initialize connection (called automatically)
  • ListTools(ctx context.Context) ([]MCPTool, error) - List remote tools
  • CallTool(ctx context.Context, name string, args map[string]interface{}) (*ToolResponse, error) - Call remote tool
  • RefreshToolCache(ctx context.Context) error - Refresh tool cache

Examples

See the examples/ directory for complete working examples:

  • examples/server/ - Basic MCP server
  • examples/client/ - MCP client connecting to remote server
  • examples/unified-server/ - Server with both local and remote tools
  • examples/object-example/ - Comprehensive object and array handling examples
  • examples/tool-discovery/ - Searchable tools and dynamic providers for reduced context usage

Searchable Tools and Tool Discovery

When working with many tools, the context window can become bloated with tool definitions. The discovery package provides searchable tools - tools that are hidden from tools/list but can be discovered via search and executed on-demand.

This approach is inspired by Anthropic's Tool Search Tool pattern, which can reduce token usage by up to 85% while maintaining access to your full tool library.

For full documentation, see the discovery package README.

Quick Example
import (
    "github.com/paularlott/mcp"
    "github.com/paularlott/mcp/discovery"
)

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

    // Create a tool registry for searchable tools
    registry := discovery.NewToolRegistry()

    // Register searchable tools (hidden from tools/list, but discoverable)
    registry.RegisterTool(
        mcp.NewTool("send_email", "Send an email",
            mcp.String("to", "Recipient", mcp.Required()),
            mcp.String("subject", "Subject", mcp.Required()),
        ),
        handleSendEmail,
        "email", "notification", "smtp", // keywords for search
    )

    // Attach to server - registers tool_search, execute_tool
    registry.Attach(server)

    // Start server - only shows tool_search, execute_tool
    // but send_email is discoverable and callable!
    http.HandleFunc("/mcp", server.HandleRequest)
    log.Fatal(http.ListenAndServe(":8000", nil))
}

The workflow for LLMs becomes:

  1. tool_search(query="email") → finds "send_email" with full schema
  2. execute_tool(name="send_email", arguments={...}) → executes the tool

OpenAI Compatibility

The openai subpackage provides types and utilities for building OpenAI-compatible APIs that use MCP tools:

import "github.com/paularlott/mcp/openai"

// Convert MCP tools to OpenAI function format
mcpTools := server.ListTools()
openAITools := openai.MCPToolsToOpenAI(mcpTools)

// Extract results from MCP tool responses for OpenAI
result, err := openai.ExtractToolResult(mcpResponse)

// Stream handling utilities
stream := openai.NewChatStream(ctx, responseChan, errorChan)
acc := &openai.CompletionAccumulator{}

See openai/README.md for complete documentation.

License

MIT License

Documentation

Index

Constants

View Source
const (
	MCPProtocolVersionLatest = "2025-06-18"
	MCPProtocolVersionMin    = "2024-11-05"
)
View Source
const (
	ErrorCodeParseError               = -32700
	ErrorCodeInvalidRequest           = -32600
	ErrorCodeMethodNotFound           = -32601
	ErrorCodeInvalidParams            = -32602
	ErrorCodeInternalError            = -32603
	ErrorCodeImplementationErrorStart = -32000
	ErrorCodeImplementationErrorEnd   = -32099
)

MCP error codes

Variables

View Source
var (
	ErrUnknownTool      = errors.New("unknown tool")
	ErrUnknownParameter = errors.New("parameter not found")
)

Functions

func NewToolError

func NewToolError(code int, message string, data interface{}) error

NewToolError creates a custom MCP error with specific code

func NewToolErrorInternal

func NewToolErrorInternal(message string) error

NewToolErrorInternal creates an error for internal server errors

func NewToolErrorInvalidParams

func NewToolErrorInvalidParams(message string) error

NewToolErrorInvalidParams creates an error for invalid parameters

Types

type AuthProvider

type AuthProvider interface {
	GetAuthHeader() (string, error)
	Refresh() error
}

AuthProvider interface for different authentication methods

type BearerTokenAuth

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

BearerTokenAuth implements simple bearer token authentication

func NewBearerTokenAuth

func NewBearerTokenAuth(token string) *BearerTokenAuth

NewBearerTokenAuth creates a new bearer token auth provider

func (*BearerTokenAuth) GetAuthHeader

func (b *BearerTokenAuth) GetAuthHeader() (string, error)

func (*BearerTokenAuth) Refresh

func (b *BearerTokenAuth) Refresh() error

type Client

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

Client represents an MCP client for connecting to remote servers

func NewClient

func NewClient(baseURL string, auth AuthProvider) *Client

NewClient creates a new MCP client

func (*Client) CallTool

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

CallTool executes a tool on the remote server

func (*Client) Initialize

func (c *Client) Initialize(ctx context.Context) error

Initialize performs the MCP handshake with the remote server

func (*Client) ListTools

func (c *Client) ListTools(ctx context.Context) ([]MCPTool, error)

ListTools retrieves tools from the remote server

func (*Client) RefreshToolCache

func (c *Client) RefreshToolCache(ctx context.Context) error

RefreshToolCache explicitly refreshes the tool cache

type MCPError

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

type MCPRequest

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

MCP Protocol types

type MCPResponse

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

type MCPTool

type MCPTool struct {
	Name         string      `json:"name"`
	Description  string      `json:"description"`
	InputSchema  interface{} `json:"inputSchema"`
	OutputSchema interface{} `json:"outputSchema,omitempty"`
}

type OAuth2Auth

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

OAuth2Auth implements OAuth2 authentication with token refresh

func NewOAuth2Auth

func NewOAuth2Auth(clientID, clientSecret, tokenURL string, scopes []string) *OAuth2Auth

NewOAuth2Auth creates a new OAuth2 auth provider

func (*OAuth2Auth) GetAuthHeader

func (o *OAuth2Auth) GetAuthHeader() (string, error)

func (*OAuth2Auth) Refresh

func (o *OAuth2Auth) Refresh() error

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option interface for parameter options

func Required

func Required() Option

type Parameter

type Parameter interface {
	// contains filtered or unexported methods
}

Parameter interface for all parameter types

func Boolean

func Boolean(name, description string, options ...Option) Parameter

Boolean creates a boolean parameter

func Number

func Number(name, description string, options ...Option) Parameter

Number creates a number parameter

func NumberArray

func NumberArray(name, description string, options ...Option) Parameter

NumberArray creates a number array parameter

func Object

func Object(name, description string, propertiesAndOptions ...interface{}) Parameter

Object creates an object parameter with properties

func ObjectArray

func ObjectArray(name, description string, propertiesAndOptions ...interface{}) Parameter

ObjectArray creates an array of objects parameter

func Output

func Output(parameters ...Parameter) Parameter

func String

func String(name, description string, options ...Option) Parameter

String creates a string parameter

func StringArray

func StringArray(name, description string, options ...Option) Parameter

StringArray creates a string array parameter

type ResourceContent

type ResourceContent struct {
	URI      string `json:"uri"`
	MimeType string `json:"mimeType,omitempty"`
	Text     string `json:"text,omitempty"`
	Blob     string `json:"blob,omitempty"` // base64 encoded
}

type ResourceResponse

type ResourceResponse struct {
	Contents []ResourceContent `json:"contents"`
}

func NewResourceResponseBlob

func NewResourceResponseBlob(uri string, data []byte, mimeType string) *ResourceResponse

func NewResourceResponseText

func NewResourceResponseText(uri, text, mimeType string) *ResourceResponse

type Server

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

Server represents an MCP server instance

func NewServer

func NewServer(name, version string) *Server

NewServer creates a new MCP server instance

func (*Server) CallTool

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

CallTool executes a tool directly with namespace support (direct API) It checks local tools first, then remote tools, then deferred/dynamic tools from the registry

func (*Server) HandleRequest

func (s *Server) HandleRequest(w http.ResponseWriter, r *http.Request)

HandleRequest handles MCP protocol requests

func (*Server) ListTools

func (s *Server) ListTools() []MCPTool

ListTools returns all registered tools including remote ones (direct API)

func (*Server) RefreshTools

func (s *Server) RefreshTools() error

RefreshTools manually refreshes the tool cache and lookup from all remote servers

func (*Server) RegisterRemoteServer

func (s *Server) RegisterRemoteServer(url, namespace string, auth AuthProvider) error

RegisterRemoteServer registers a remote MCP server

func (*Server) RegisterTool

func (s *Server) RegisterTool(tool *ToolBuilder, handler ToolHandler)

RegisterTool registers a new tool with the server

func (*Server) SetInstructions

func (s *Server) SetInstructions(instructions string)

type ToolBuilder

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

ToolBuilder provides fluent API for building tools

func NewTool

func NewTool(name, description string, parameters ...Parameter) *ToolBuilder

NewTool creates a new tool with the declarative API

func (*ToolBuilder) BuildOutputSchema

func (t *ToolBuilder) BuildOutputSchema() map[string]interface{}

BuildOutputSchema is a public method for building the output schema

func (*ToolBuilder) BuildSchema

func (t *ToolBuilder) BuildSchema() map[string]interface{}

BuildSchema is a public method for building the input schema

func (*ToolBuilder) Description added in v0.6.0

func (t *ToolBuilder) Description() string

Description returns the tool's description

func (*ToolBuilder) Name added in v0.6.0

func (t *ToolBuilder) Name() string

Name returns the tool's name

type ToolCallParams

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

type ToolContent

type ToolContent struct {
	Type     string           `json:"type"`
	Text     string           `json:"text,omitempty"`
	Data     string           `json:"data,omitempty"`
	MimeType string           `json:"mimeType,omitempty"`
	Resource *ResourceContent `json:"resource,omitempty"`
}

type ToolError

type ToolError struct {
	Code    int
	Message string
	Data    interface{}
}

ToolError represents an MCP protocol error that can be returned from tool handlers

func (*ToolError) Error

func (e *ToolError) Error() string

type ToolHandler

type ToolHandler func(ctx context.Context, req *ToolRequest) (*ToolResponse, error)

ToolHandler represents a function that handles tool calls

type ToolRequest

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

ToolRequest provides typed access to tool arguments

func NewToolRequest added in v0.6.0

func NewToolRequest(args map[string]interface{}) *ToolRequest

NewToolRequest creates a new ToolRequest with the given arguments

func (*ToolRequest) Args added in v0.6.3

func (r *ToolRequest) Args() map[string]interface{}

Args returns all arguments as a map

func (*ToolRequest) Bool

func (r *ToolRequest) Bool(name string) (bool, error)

func (*ToolRequest) BoolOr

func (r *ToolRequest) BoolOr(name string, defaultValue bool) bool

func (*ToolRequest) Float

func (r *ToolRequest) Float(name string) (float64, error)

func (*ToolRequest) FloatOr

func (r *ToolRequest) FloatOr(name string, defaultValue float64) float64

func (*ToolRequest) FloatSlice

func (r *ToolRequest) FloatSlice(name string) ([]float64, error)

func (*ToolRequest) FloatSliceOr

func (r *ToolRequest) FloatSliceOr(name string, defaultValue []float64) []float64

func (*ToolRequest) GetObjectBoolProperty

func (r *ToolRequest) GetObjectBoolProperty(objectName, propertyName string) (bool, error)

GetObjectBoolProperty extracts a bool property from an object parameter

func (*ToolRequest) GetObjectIntProperty

func (r *ToolRequest) GetObjectIntProperty(objectName, propertyName string) (int, error)

GetObjectIntProperty extracts an int property from an object parameter

func (*ToolRequest) GetObjectProperty

func (r *ToolRequest) GetObjectProperty(objectName, propertyName string) (interface{}, error)

GetObjectProperty extracts a property from an object parameter

func (*ToolRequest) GetObjectStringProperty

func (r *ToolRequest) GetObjectStringProperty(objectName, propertyName string) (string, error)

GetObjectStringProperty extracts a string property from an object parameter

func (*ToolRequest) Int

func (r *ToolRequest) Int(name string) (int, error)

func (*ToolRequest) IntOr

func (r *ToolRequest) IntOr(name string, defaultValue int) int

func (*ToolRequest) IntSlice

func (r *ToolRequest) IntSlice(name string) ([]int, error)

func (*ToolRequest) IntSliceOr

func (r *ToolRequest) IntSliceOr(name string, defaultValue []int) []int

func (*ToolRequest) Object

func (r *ToolRequest) Object(name string) (map[string]interface{}, error)

Object returns a parameter as a map[string]interface{} (generic object)

func (*ToolRequest) ObjectOr

func (r *ToolRequest) ObjectOr(name string, defaultValue map[string]interface{}) map[string]interface{}

ObjectOr returns a parameter as an object or the default value

func (*ToolRequest) ObjectSlice

func (r *ToolRequest) ObjectSlice(name string) ([]map[string]interface{}, error)

ObjectSlice returns a parameter as a slice of objects

func (*ToolRequest) ObjectSliceOr

func (r *ToolRequest) ObjectSliceOr(name string, defaultValue []map[string]interface{}) []map[string]interface{}

ObjectSliceOr returns a parameter as a slice of objects or the default value

func (*ToolRequest) String

func (r *ToolRequest) String(name string) (string, error)

func (*ToolRequest) StringOr

func (r *ToolRequest) StringOr(name, defaultValue string) string

func (*ToolRequest) StringSlice

func (r *ToolRequest) StringSlice(name string) ([]string, error)

func (*ToolRequest) StringSliceOr

func (r *ToolRequest) StringSliceOr(name string, defaultValue []string) []string

type ToolResponse

type ToolResponse struct {
	Content           []ToolContent `json:"content"`
	StructuredContent interface{}   `json:"structuredContent,omitempty"`
}

ToolResponse represents the response from a tool

func NewToolResponseAudio

func NewToolResponseAudio(data []byte, mimeType string) *ToolResponse

func NewToolResponseImage

func NewToolResponseImage(data []byte, mimeType string) *ToolResponse

func NewToolResponseJSON

func NewToolResponseJSON(data interface{}) *ToolResponse

func NewToolResponseMulti

func NewToolResponseMulti(responses ...*ToolResponse) *ToolResponse

func NewToolResponseResource

func NewToolResponseResource(uri, text, mimeType string) *ToolResponse
func NewToolResponseResourceLink(uri, text string) *ToolResponse

func NewToolResponseStructured

func NewToolResponseStructured(data interface{}) *ToolResponse

func NewToolResponseText

func NewToolResponseText(text string) *ToolResponse

type ToolResult

type ToolResult struct {
	Content           []ToolContent `json:"content,omitempty"`
	StructuredContent interface{}   `json:"structuredContent,omitempty"`
	IsError           bool          `json:"isError,omitempty"`
}

Directories

Path Synopsis
Package discovery provides tool discovery functionality for MCP servers.
Package discovery provides tool discovery functionality for MCP servers.
examples
client command
object-example command
server command
tool-discovery command
unified-server command
example command

Jump to

Keyboard shortcuts

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