mcp-grpc运输
一种MCP传输,它使用gRPC在客户端和MCP服务器之间进行通信。这将使在远程实例上托管MCP服务器变得更容易,就像任何其他web服务器一样。
使用gRPC作为传输,允许重用任何现有的基础设施和流程进行身份验证和授权。
\[!注意\] 仅支持目前使用最广泛的两个库: - mark3labs/mcp go
用法
随着 metoro-io 图书馆
\[!提示\] 完整示例可在 examples/metoro-io-server/main.go在 metoro-io/mcp-golang 服务器示例,只需按如下方式更换运输工具:
+ import grpctransport "github.com/rustycl0ck/mcp-grpc-transport/pkg/metoro-io-transport/grpc"
func main() {
...
- server := mcp_golang.NewServer(stdio.NewStdioServerTransport())
+ server := mcp_golang.NewServer(grpctransport.NewGrpcServerTransport())
...
}如果需要,您可以使用更多选项自定义服务器:
import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main(){
serverTransport := grpctransport.NewGrpcServerTransport(
grpctransport.WithPort(10051),
grpctransport.WithGrpcOpts(
grpc.UnaryInterceptor(loggingInterceptor),
grpc.MaxRecvMsgSize(1024*1024), // 1MB
grpc.Creds(insecure.NewCredentials()), // TODO: Use TLS in production!
),
)
// Create a new server with the transport
server := mcp_golang.NewServer(serverTransport)
}随着 mark3labs 图书馆
\[!提示\] 完整示例可在 examples/mark3labs-server/main.go在 mark3labs/mcp-go 服务器示例,只需按如下方式更换运输工具:
+ import grpctransport "github.com/rustycl0ck/mcp-grpc-transport/pkg/mark3labs-transport/grpc"
func main() {
...
// Start the server
- if err := server.ServeStdio(s); err != nil {
- fmt.Printf("Server error: %v\n", err)
- }
+ srv := grpctransport.NewGrpcServer(s)
+ if err := srv.Listen(context.Background()); err != nil {
+ fmt.Printf("Server error: %v\n", err)
+ }
...
}如果需要,您可以使用更多选项自定义服务器:
import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main(){
- srv := grpctransport.NewGrpcServer(s)
+ srv := grpctransport.NewGrpcServer(s,
grpctransport.WithPort(10051),
grpctransport.WithGrpcOpts(
grpc.UnaryInterceptor(loggingInterceptor),
grpc.MaxRecvMsgSize(1024*1024), // 1MB
grpc.Creds(insecure.NewCredentials()), // TODO: Use TLS in production!
),
)
}示例
启动服务器:
$ go run examples/metoro-io-server.go
transport started...
Received message...
transport started...
Received message...
配置您的IDE客户端(以下是对Cursor IDE的困惑 mcp.json)
{
"mcpServers": {
"my-mcp-server": {
"command": "go",
"args": [
"run",
"github.com/rustycl0ck/mcp-grpc-transport/cmd/client@latest",
"--address",
"localhost:50051" // Replace with actual server location if hosted remotely
]
}
}
}或者直接通过CLI在本地测试客户端:
$ echo '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | go run github.com/rustycl0ck/mcp-grpc-transport/cmd/client@latest
{"id":1,"jsonrpc":"2.0","result":{"tools":[{"description":"Get the weather forecast for temperature, wind speed and relative humidity","inputSchema":{"$schema":"https://json-schema.org/draft/2020-12/schema","properties":{"latitude":{"description":"The latitude of the location to get the weather for","type":"number"},"longitude":{"description":"The longitude of the location to get the weather for","type":"number"}},"required":["longitude","latitude"],"type":"object"},"name":"get_weather"},{"description":"Says hello","inputSchema":{"$schema":"https://json-schema.org/draft/2020-12/schema","properties":{"name":{"description":"The name to say hello to","type":"string"}},"required":["name"],"type":"object"},"name":"hello"}]}}