structured_input_and_output

command
v0.39.1 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2025 License: MIT Imports: 5 Imported by: 0

README

Structured Content Example

This example shows how to return structuredContent in tool result with corresponding OutputSchema.

Defined in the MCP spec here: https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content

Usage

Define a struct for your input:

type WeatherRequest struct {
    Location string `json:"location,required" jsonschema_description:"City or location"`
    Units    string `json:"units,omitempty" jsonschema_description:"celsius or fahrenheit" jsonschema:"enum=celsius,enum=fahrenheit"`
}

Define a struct for your output:

type WeatherResponse struct {
    Location    string  `json:"location" jsonschema_description:"The location"`
    Temperature float64 `json:"temperature" jsonschema_description:"Current temperature"`
    Conditions  string  `json:"conditions" jsonschema_description:"Weather conditions"`
}

Add it to your tool:

tool := mcp.NewTool("get_weather",
    mcp.WithDescription("Get weather information"),
    mcp.WithInputSchema[WeatherRequest](),
    mcp.WithOutputSchema[WeatherResponse](),
)

Return structured data in tool result:

func weatherHandler(ctx context.Context, request mcp.CallToolRequest, args WeatherRequest) (*mcp.CallToolResult, error) {
    response := WeatherResponse{
        Location:    args.Location,
        Temperature: 25.0,
        Conditions:  "Cloudy",
    }
    
    fallbackText := fmt.Sprintf("Weather in %s: %.1f°C, %s", 
        response.Location, response.Temperature, response.Conditions)
    
    return mcp.NewToolResultStructured(response, fallbackText), nil
}

See main.go for more examples.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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