mcp-go icon indicating copy to clipboard operation
mcp-go copied to clipboard

Can not connect sse server

Open jyz0309 opened this issue 8 months ago • 2 comments

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

Image

jyz0309 avatar Mar 19 '25 08:03 jyz0309

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. ^^

grootpiano avatar Mar 20 '25 02:03 grootpiano

https://github.com/mark3labs/mcp-go/pull/63

a67793581 avatar Mar 21 '25 08:03 a67793581

Fixed in #63

ezynda3 avatar Mar 24 '25 16:03 ezynda3