Lists files in the current directory
Foxy Contexts is a powerful Golang library that enables developers to build context servers supporting the Model Context Protocol (MCP) using a declarative approach. It simplifies the creation of MCP servers by providing a structured way to define tools, resources, and prompts, while leveraging dependency injection for efficient component reuse. With Foxy Contexts, developers can easily organize their code by colocating logic and definitions for each tool, resource, or prompt in separate places, making the codebase more maintainable. The library is built on top of Uber's fx dependency injection framework, allowing for flexible integration of shared components like clients and database connections.
Foxy Contexts is a Golang library for building Model Context Protocol (MCP) servers using a declarative approach. This guide will help you get started with setting up and using Foxy Contexts in your projects.
To use Foxy Contexts in your Go project, you need to install it using Go modules:
go get github.com/strowk/foxy-contexts
Here's how to create a simple MCP server with Foxy Contexts:
import (
"github.com/strowk/foxy-contexts/pkg/app"
"github.com/strowk/foxy-contexts/pkg/fxctx"
"github.com/strowk/foxy-contexts/pkg/mcp"
"github.com/strowk/foxy-contexts/pkg/stdio"
"go.uber.org/fx"
)
// Example of defining a tool
func NewMyTool() fxctx.Tool {
return fxctx.NewTool(
&mcp.Tool{
Name: "my-tool",
Description: Ptr("Description of my tool"),
InputSchema: mcp.ToolInputSchema{
Type: "object",
Properties: map[string]map[string]interface{}{
// Define your input properties here
},
Required: []string{},
},
},
func(ctx context.Context, args map[string]interface{}) *mcp.CallToolResult {
// Implement your tool logic here
return &mcp.CallToolResult{
Content: []interface{}{
mcp.TextContent{
Type: "text",
Text: "Result from my tool",
},
},
}
},
)
}
func main() {
builder := app.NewBuilder()
// Register your tools, resources, or prompts
builder.RegisterTool(NewMyTool)
// Configure transports (stdio, SSE, or HTTP)
builder.WithStdioTransport()
// Build and run the application
builder.Build().Run()
}
Foxy Contexts supports multiple transport mechanisms:
Stdio Transport: For command-line based interactions
builder.WithStdioTransport()
SSE Transport: For Server-Sent Events
builder.WithSSETransport()
HTTP Transport: For HTTP-based communication (beta)
builder.WithHTTPTransport()
Foxy Contexts provides a testing package called foxytest
for functional testing:
import "github.com/strowk/foxy-contexts/pkg/foxytest"
func TestMyTool(t *testing.T) {
tester := foxytest.NewTester(t)
defer tester.Close()
// Register your tool
tester.RegisterTool(NewMyTool)
// Test your tool
result := tester.CallTool("my-tool", map[string]interface{}{
// Tool arguments
})
// Assert on the result
// ...
}
Foxy Contexts supports various MCP features:
Check the official documentation and examples for more detailed information on these features.