package mainimport ("context""fmt""log""github.com/mark3labs/mcp-go/mcp""github.com/mark3labs/mcp-go/server")type MCPServer struct {server *server.MCPServer}func NewMCPServer() *MCPServer {mcpServer := server.NewMCPServer("example-server","1.0.0",server.WithResourceCapabilities(true, true),server.WithPromptCapabilities(true),server.WithToolCapabilities(true),)// Add echo toolmcpServer.AddTool(mcp.NewTool("echo",mcp.WithDescription("Echo back the input"),mcp.WithString("message",mcp.Required(),mcp.Description("Message to echo back"),),), echoHandler)return &MCPServer{server: mcpServer,}}func main() {s := NewMCPServer()sseServer := s.ServeSSE("localhost:8080")log.Printf("SSE server listening on :8080")if err := sseServer.Start(":8080"); err != nil {log.Fatalf("Server error: %v", err)}}func echoHandler(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {msg, ok := req.Params.Arguments["message"].(string)if !ok {return nil, fmt.Errorf("invalid message parameter")}return mcp.NewToolResultText(fmt.Sprintf("Echo: %s", msg)), nil}func (s *MCPServer) ServeSSE(addr string) *server.SSEServer {return server.NewSSEServer(s.server,server.WithBaseURL(fmt.Sprintf("http://%s", addr)),)}
func NewSSEServer(server *MCPServer, opts ...SSEOption) *SSEServer {s := &SSEServer{server: server,sseEndpoint: "/sse",messageEndpoint: "/message",useFullURLForMessageEndpoint: true,}
curl 'http://localhost:8080/sse'event: endpointdata: http://localhost:8080/message?sessionId=91c61b81-a84d-465b-89e4-f9ebd244c958
curl -X POST 'http://localhost:8080/message?sessionId=dd92c1c3-62b3-47d8-bf79-5be071839779'{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}curl -X POST 'http://localhost:8080/message?sessionId=265887bb-7193-490c-a25c-6644aa6ec8cd' -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":null,"method":"tools/call","params":{"name":"echo","arguments":{"message":"Hello SSE!"}}}'
package mainimport ("context""encoding/json""fmt""log""time""github.com/mark3labs/mcp-go/client""github.com/mark3labs/mcp-go/mcp")func main() {ctx := context.Background()client, err := client.NewSSEMCPClient("http://localhost:8080/sse")if err != nil {log.Fatalf("Failed to create SSE MCP client: %v", err)}err = client.Start(ctx)if err != nil {log.Fatalf("Failed to start SSE MCP client: %v", err)}// InitializeinitRequest := mcp.InitializeRequest{}initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSIONinitRequest.Params.ClientInfo = mcp.Implementation{Name: "test-client",Version: "1.0.0",}_, err = client.Initialize(ctx, initRequest)if err != nil {log.Fatalf("Failed to Initialize SSE MCP client: %v", err)}request := mcp.CallToolRequest{Request: mcp.Request{Method: "tools/call",},}arguments := map[string]interface{}{"message": "Hello SSE!",}request.Params.Name = "echo"request.Params.Arguments = argumentsd, _ := json.Marshal(request)fmt.Println(string(d))// Test echo toolresult, err := client.CallTool(context.Background(), request)if err != nil {return}textContent := result.Content[0].(mcp.TextContent)fmt.Println(textContent.Text)time.Sleep(100 * time.Second)}
{"method":"tools/call","params":{"name":"echo","arguments":{"message":"Hello SSE!"}}}Echo: Hello SSE!
func (c *SSEMCPClient) Start(ctx context.Context) error {req, err := http.NewRequestWithContext(ctx, "GET", c.baseURL.String(), nil)
func (c *SSEMCPClient) readSSE(reader io.ReadCloser) {if event != "" && data != "" {c.handleSSEEvent(event, data)}
func (c *SSEMCPClient) handleSSEEvent(event, data string) {switch event {case "endpoint":endpoint, err := c.baseURL.Parse(data)c.endpoint = endpointcase "message":for _, handler := range c.notifications {handler(notification)}
func (c *SSEMCPClient) CallTool(ctx context.Context,request mcp.CallToolRequest,) (*mcp.CallToolResult, error) {response, err := c.sendRequest(ctx, "tools/call", request.Params)
func (c *SSEMCPClient) sendRequest(ctx context.Context,method string,params interface{},) (*json.RawMessage, error) {req, err := http.NewRequestWithContext(ctx,"POST",c.endpoint.String(),bytes.NewReader(requestBytes),)


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




