Skip to content

feature: support creating tools using go-struct-style input schema #535

@Grivn

Description

@Grivn

Problem Statement

Since we’ve already implemented the output schema with structured content, I’m now working on defining the input schema in a similar style.

We can specify JSON Schema properties directly in our Go structsq, such as required, and then use a helper like mcp.WithInputSchema[WeatherRequest]() to generate the input schema accordingly.

This enhancement will help developers maintain MCP services more effectively by enabling a consistent, structured approach to both input and output schemas.

Example 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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions