mcp-go
mcp-go copied to clipboard
Can not connect sse server
This is my test code
package main
import (
"context"
"fmt"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
func main() {
// Create MCP server
s := server.NewMCPServer(
"Demo 🚀",
"1.0.0",
)
// Add tool
tool := mcp.NewTool("hello_world",
mcp.WithDescription("Say hello to someone"),
mcp.WithString("name",
mcp.Required(),
mcp.Description("Name of the person to greet"),
),
)
// Add tool handler
s.AddTool(tool, helloHandler)
// Start the stdio server
/*if err := server.ServeStdio(s); err != nil {
fmt.Printf("Server error: %v\n", err)
}*/
sse := server.NewSSEServer(s, server.WithBasePath("http://localhost:5000"))
err := sse.Start(":5000")
if err != nil {
fmt.Printf("Server error: %v\n", err)
}
}
func helloHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
name, ok := request.Params.Arguments["name"].(string)
if !ok {
return mcp.NewToolResultError("name must be a string"), nil
}
return mcp.NewToolResultText(fmt.Sprintf("Hello, %s!", name)), nil
}
When I use MCP Inspector to connect to server, the error show that connection failed
Hi @jyz0309 , please replace server.WithBasePath("http://localhost:5000") with server.WithBaseURL("http://localhost:5000") and try it again.
If it still can't connect, please share some MCP Inspector's error logs with us, then we can know the exact error reason. ^^
https://github.com/mark3labs/mcp-go/pull/63
Fixed in #63