fastmcp

package
v0.0.0-...-93b92dd Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package fastmcp provides a simplified interface for creating MCP servers.

FastMCP reduces boilerplate code and provides a fluent interface for configuring servers. It wraps the core MCP server implementation with a more ergonomic API while maintaining full protocol compatibility.

Basic usage:

app := fastmcp.New("My App")

// Add tools
app.Tool("greet", func(name string) string {
    return "Hello, " + name + "!"
}, "Greet a person")

// Add async tools
app.AsyncTool("longProcess", func(params string) error {
    // Long-running operation
    return nil
}, "Process data asynchronously")

// Add resources
app.Resource("files/{path}", func(path string) ([]byte, error) {
    return ioutil.ReadFile(path)
}, "Access files")

// Add prompts
app.Prompt("confirm", func(action string) []protocol.PromptMessage {
    return []protocol.PromptMessage{
        {
            Role: protocol.RoleAssistant,
            Content: protocol.TextContent{
                Type: "text",
                Text: fmt.Sprintf("Are you sure you want to %s?", action),
            },
        },
    }
}, "Confirmation prompt")

// Run with stdio transport
if err := app.RunStdio(); err != nil {
    log.Fatal(err)
}

Server Capabilities:

FastMCP automatically configures default server capabilities:

  • Tool list change notifications
  • Resource subscriptions and list change notifications
  • Prompt list change notifications
  • Logging support

You can customize capabilities when creating a new FastMCP instance:

app := fastmcp.New("My App", server.WithCapabilities(protocol.ServerCapabilities{
    Tools: &protocol.ToolsCapability{
        ListChanged: boolPtr(true),
    },
    Resources: &protocol.ResourcesCapability{
        Subscribe:   boolPtr(true),
        ListChanged: boolPtr(true),
    },
}))

Transport Options:

// Run with stdio (for CLI apps)
app.RunStdio()

// Run with WebSocket (for web apps)
app.RunWebSocket(":8080")

// Run with SSE (for web browsers)
app.RunSSE(":8080")

The FastMCP API is designed to be chainable:

fastmcp.New("My App").
    Tool("tool1", handler1, "desc1").
    Tool("tool2", handler2, "desc2").
    Resource("res1", handler3, "desc3").
    RunStdio()

Handler Function Types:

FastMCP supports various handler function signatures:

// Simple tools
func(string) string
func(int) (int, error)

// Complex tools with structs
func(User) (*User, error)
func(SearchParams) ([]Result, error)

// Resources with parameters
func(path string) ([]byte, error)
func(id string, version int) (interface{}, error)

// Prompts with multiple messages
func(context string) []protocol.PromptMessage
func(action string) protocol.TextContent

For more control, you can access the underlying server:

app := fastmcp.New("My App")
server := app.Server()

FastMCP is ideal for building MCP applications quickly while maintaining access to the full power of the protocol when needed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FastMCP

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

FastMCP provides a simplified interface for creating MCP servers

func New

func New(name string, options ...server.ServerOption) *FastMCP

New creates a new FastMCP instance with default capabilities

func (*FastMCP) AsyncTool

func (f *FastMCP) AsyncTool(name string, handler interface{}, description string) *FastMCP

AsyncTool registers an asynchronous tool with the server

func (*FastMCP) Prompt

func (f *FastMCP) Prompt(name string, handler interface{}, description string) *FastMCP

Prompt registers a prompt with the server

func (*FastMCP) Resource

func (f *FastMCP) Resource(pattern string, handler interface{}, description string) *FastMCP

Resource registers a resource with the server

func (*FastMCP) RunSSE

func (f *FastMCP) RunSSE(addr string) error

RunSSE starts the server with SSE transport

func (*FastMCP) RunStdio

func (f *FastMCP) RunStdio() error

RunStdio starts the server with stdio transport

func (*FastMCP) RunWebSocket

func (f *FastMCP) RunWebSocket(addr string) error

RunWebSocket starts the server with WebSocket transport

func (*FastMCP) Server

func (f *FastMCP) Server() *server.Server

Server returns the underlying server instance

func (*FastMCP) Tool

func (f *FastMCP) Tool(name string, handler interface{}, description string) *FastMCP

Tool registers a synchronous tool with the server

Jump to

Keyboard shortcuts

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