package mainimport (example "learn/langchain/protoc_gen_mcp/exp1/server""github.com/mark3labs/mcp-go/server")func main() {s := server.NewMCPServer("My Server", // Server 名称"1.0.0", // 版本号)example.RegisterExampleService(s, &example.Server{})// 创建基于 SSE 的服务器实例sseServer := server.NewSSEServer(s)// 启动服务器,监听指定端口(如 :8080)err := sseServer.Start(":8083")if err != nil {panic(err)}}
nvm use v18.14.0npx @modelcontextprotocol/inspector

example_ExampleService_GetPerson
{person:{name:"hello"id:123email:"example@example.com"}}
// Code generated by protoc-gen-mcp-go. DO NOT EDIT.// source: example_service.protopackage example_service_v1mcpimport (example_service_v1 "learn/langchain/protoc_gen_mcp/exp1/example.service.v1")import ("context""github.com/mark3labs/mcp-go/mcp"mcpserver "github.com/mark3labs/mcp-go/server""encoding/json""google.golang.org/protobuf/encoding/protojson""connectrpc.com/connect"grpc "google.golang.org/grpc")var (ExampleService_GetPersonTool = mcp.Tool{Name: "example_ExampleService_GetPerson", Description: "", InputSchema: mcp.ToolInputSchema{Type: "", Properties: map[string]interface{}(nil), Required: []string(nil)}, RawInputSchema: json.RawMessage{0x7b, 0x22, 0x70, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x69, 0x65, 0x73, 0x22, 0x3a, 0x7b, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x7b, 0x22, 0x74, 0x79, 0x70, 0x65, 0x22, 0x3a, 0x22, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x22, 0x7d, 0x7d, 0x2c, 0x22, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x22, 0x3a, 0x5b, 0x5d, 0x2c, 0x22, 0x74, 0x79, 0x70, 0x65, 0x22, 0x3a, 0x22, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x22, 0x7d}})// ExampleServiceServer is compatible with the grpc-go server interface.type ExampleServiceServer interface {GetPerson(ctx context.Context, req *example_service_v1.PersonRequest) (*example_service_v1.PersonResponse, error)}func RegisterExampleServiceHandler(s *mcpserver.MCPServer, srv ExampleServiceServer) {s.AddTool(ExampleService_GetPersonTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {marshaled, err := json.Marshal(request.Params.Arguments)if err != nil {return nil, err}var req example_service_v1.PersonRequestif err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(marshaled, &req); err != nil {return nil, err}resp, err := srv.GetPerson(ctx, &req)if err != nil {return nil, err}marshaled, err = (protojson.MarshalOptions{UseProtoNames: true, EmitDefaultValues: true}).Marshal(resp)if err != nil {return nil, err}return mcp.NewToolResultText(string(marshaled)), nil})}// ExampleServiceClient is compatible with the grpc-go client interface.type ExampleServiceClient interface {GetPerson(ctx context.Context, req *example_service_v1.PersonRequest, opts ...grpc.CallOption) (*example_service_v1.PersonResponse, error)}// ConnectExampleServiceClient is compatible with the connectrpc-go client interface.type ConnectExampleServiceClient interface {GetPerson(ctx context.Context, req *connect.Request[example_service_v1.PersonRequest]) (*connect.Response[example_service_v1.PersonResponse], error)}// ForwardToConnectExampleServiceClient registers a connectrpc client, to forward MCP calls to it.func ForwardToConnectExampleServiceClient(s *mcpserver.MCPServer, client ConnectExampleServiceClient) {s.AddTool(ExampleService_GetPersonTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {marshaled, err := json.Marshal(request.Params.Arguments)if err != nil {return nil, err}var req example_service_v1.PersonRequestif err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(marshaled, &req); err != nil {return nil, err}resp, err := client.GetPerson(ctx, connect.NewRequest(&req))if err != nil {return nil, err}marshaled, err = (protojson.MarshalOptions{UseProtoNames: true, EmitDefaultValues: true}).Marshal(resp.Msg)if err != nil {return nil, err}return mcp.NewToolResultText(string(marshaled)), nil})}// ForwardToExampleServiceClient registers a gRPC client, to forward MCP calls to it.func ForwardToExampleServiceClient(s *mcpserver.MCPServer, client ExampleServiceClient) {s.AddTool(ExampleService_GetPersonTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {marshaled, err := json.Marshal(request.Params.Arguments)if err != nil {return nil, err}var req example_service_v1.PersonRequestif err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(marshaled, &req); err != nil {return nil, err}resp, err := client.GetPerson(ctx, &req)if err != nil {return nil, err}marshaled, err = (protojson.MarshalOptions{UseProtoNames: true, EmitDefaultValues: true}).Marshal(resp)if err != nil {return nil, err}return mcp.NewToolResultText(string(marshaled)), nil})}
RegisterExampleServiceHandlerForwardToConnectExampleServiceClientForwardToExampleServiceClient
s.AddTool(ExampleService_GetPersonTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {marshaled, err := json.Marshal(request.Params.Arguments)if err != nil {return nil, err}var req example_service_v1.PersonRequestif err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(marshaled, &req); err != nil {return nil, err}


文章转载自golang算法架构leetcode技术php,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




