Skip to content

Commit 89a0d50

Browse files
committed
examples/http: add HTTP server example with built-in client
This example demonstrates MCP over HTTP using streamable transport, including both server and client implementations. The server provides a cityTime tool that returns current time for major US cities. Fixes modelcontextprotocol#168
1 parent a617dce commit 89a0d50

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

examples/http/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,29 @@ type GetTimeParams struct {
6464
}
6565

6666
// getTime implements the tool that returns the current time for a given city.
67-
func getTime(ctx context.Context, ss *mcp.ServerSession, params *mcp.CallToolParamsFor[GetTimeParams]) (*mcp.CallToolResultFor[any], error) {
67+
func getTime(ctx context.Context, req *mcp.CallToolRequest, params GetTimeParams) (*mcp.CallToolResult, any, error) {
6868
// Define time zones for each city
6969
locations := map[string]string{
7070
"nyc": "America/New_York",
7171
"sf": "America/Los_Angeles",
7272
"boston": "America/New_York",
7373
}
7474

75-
city := params.Arguments.City
75+
city := params.City
7676
if city == "" {
7777
city = "nyc" // Default to NYC
7878
}
7979

8080
// Get the timezone.
8181
tzName, ok := locations[city]
8282
if !ok {
83-
return nil, fmt.Errorf("unknown city: %s", city)
83+
return nil, nil, fmt.Errorf("unknown city: %s", city)
8484
}
8585

8686
// Load the location.
8787
loc, err := time.LoadLocation(tzName)
8888
if err != nil {
89-
return nil, fmt.Errorf("failed to load timezone: %w", err)
89+
return nil, nil, fmt.Errorf("failed to load timezone: %w", err)
9090
}
9191

9292
// Get current time in that location.
@@ -103,11 +103,11 @@ func getTime(ctx context.Context, ss *mcp.ServerSession, params *mcp.CallToolPar
103103
cityNames[city],
104104
now.Format(time.RFC3339))
105105

106-
return &mcp.CallToolResultFor[any]{
106+
return &mcp.CallToolResult{
107107
Content: []mcp.Content{
108108
&mcp.TextContent{Text: response},
109109
},
110-
}, nil
110+
}, nil, nil
111111
}
112112

113113
func runServer(url string) {
@@ -155,7 +155,7 @@ func runClient(url string) {
155155
}, nil)
156156

157157
// Connect to the server.
158-
session, err := client.Connect(ctx, transport)
158+
session, err := client.Connect(ctx, transport, nil)
159159
if err != nil {
160160
log.Fatalf("Failed to connect: %v", err)
161161
}

0 commit comments

Comments
 (0)