MCP服务器构建库

Foxy Contexts是一个用于构建支持Model Context Protocol的上下文服务器的Golang库,采用声明式方法定义工具、资源和提示。

  • by strowk
  • Go
  • 本地部署
  • 114 星标
  • 更新于
在 GitHub 查看 strowk/foxy-contexts

使用说明


Foxy Contexts

Build MCP Servers Declaratively in Golang

Features 🦊 Tool Example 🦊 Docs 🦊 Sources

Foxy contexts是一个Golang库,用于构建支持 模型上下文协议.

此库仅支持协议的服务器端。使用它,您可以使用声明性方法构建上下文服务器,通过定义 工具, 资源 和 鼓励 然后在你的应用程序中注册它们。Builder正在使用 优步外汇 应用程序在引擎盖下,你也可以注射到它的容器里。

通过这种方法,您可以轻松地将工具/资源/提示的调用/读取/获取逻辑和定义放在一个单独的地方,而依赖注入允许您重用共享部分,如客户端、数据库连接等。

特性

以下是已实施和计划的功能列表:

  • [x] 基础(生命周期/ping)
    • []进度(计划)
  • [x] 运输
    • [x] 标准运输
    • [x] 苏格兰和南方能源公司运输
    • [x] 流式HTTP传输(测试版)
  • [x] 工具
    • [x] 打包工具输入有助于定义工具输入模式并验证到达的输入
  • []资源
    • [x] 资源-静态
    • [x] 资源-通过资源提供者动态提供
    • []资源-通过资源模板动态(计划)
    • []资源模板完成(计划中)
    • []资源订阅
  • [x] 提示
  • [x] 提示完成
  • [x] 功能测试包foxytest
  • [x] 使用依赖注入功能简单构建MCP服务器
  • []通过MCP记录(计划)
  • []取样(计划)
  • []根(计划中)
  • []分页(计划中)
  • []通知列表_已更改(计划中)
  • [x] 测试-使用foxytest软件包进行功能测试

检查 文档 和 示例 了解更多。

工具示例

例如,尝试以下操作

git clone https://github.com/strowk/foxy-contexts
cd foxy-contexts/examples/list_current_dir_files_tool
npx @modelcontextprotocol/inspector go run main.go

,然后在浏览器打开时启动检查器http://localhost:6274并尝试使用列表当前dir文件。

以下是该示例的代码 示例/list_current_dir_files_tool/main.go (在实际应用中,您可能希望将其拆分为多个文件):

package main

import (
	"fmt"
	"os"

	"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"
	"go.uber.org/fx/fxevent"
	"go.uber.org/zap"
)

// This example defines list-current-dir-files tool for MCP server, that prints files in the current directory
// , run it with:
// npx @modelcontextprotocol/inspector go run main.go
// , then in browser open http://localhost:6274
// , then click Connect
// , then click List Tools
// , then click list-current-dir-files

// NewListCurrentDirFilesTool defines a tool that lists files in the current directory
func NewListCurrentDirFilesTool() fxctx.Tool {
	return fxctx.NewTool(
		// This information about the tool would be used when it is listed:
		&mcp.Tool{
			Name:        "list-current-dir-files",
			Description: Ptr("Lists files in the current directory"),
			InputSchema: mcp.ToolInputSchema{
				Type:       "object",
				Properties: map[string]map[string]interface{}{},
				Required:   []string{},
			},
		},

		// This is the callback that would be executed when the tool is called:
		func(_ context.Context, args map[string]interface{}) *mcp.CallToolResult {
			files, err := os.ReadDir(".")
			if err != nil {
				return &mcp.CallToolResult{
					IsError: Ptr(true),
					Meta:    map[string]interface{}{},
					Content: []interface{}{
						mcp.TextContent{
							Type: "text",
							Text: fmt.Sprintf("failed to read dir: %v", err),
						},
					},
				}
			}
			var contents []interface{} = make([]interface{}, len(files))
			for i, f := range files {
				contents[i] = mcp.TextContent{
					Type: "text",
					Text: f.Name(),
				}
			}

			return &mcp.CallToolResult{
				Meta:    map[string]interface{}{},
				Content: contents,
				IsError: Ptr(false),
			}
		},
	)
}

func main() {
	app.
		NewBuilder().
		// adding the tool to the app
		WithTool(NewListCurrentDirFilesTool).
		// setting up server
		WithName("list-current-dir-files").
		WithVersion("0.0.1").
		WithTransport(stdio.NewTransport()).
		// Configuring fx logging to only show errors
		WithFxOptions(fx.Provide(func() *zap.Logger {
				cfg := zap.NewDevelopmentConfig()
				cfg.Level.SetLevel(zap.ErrorLevel)
				logger, _ := cfg.Build()
				return logger
			}),
			fx.Option(fx.WithLogger(
				func(logger *zap.Logger) fxevent.Logger {
					return &fxevent.ZapLogger{Logger: logger}
				},
			)),
		).
		Run()
}

func Ptr[T any](v T) *T {
	return &v
}