-
Notifications
You must be signed in to change notification settings - Fork 285
mcp/examples: HTTP server example with a simple client built-in #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
60422ed
HTTP example.
wkoszek 2b21dbb
Add more details to README.
wkoszek 8d8a0b6
Add logging to make sure we know what's going on
wkoszek 45bff55
Fix arguments and log formatting.
wkoszek f832730
Split up logging middleware.
wkoszek f3d0ada
get_time -> cityTime
wkoszek d5c9351
Format.
wkoszek e312ab5
README updates.
wkoszek 1e2c65c
Dots
wkoszek cdb64ce
Accept URL
wkoszek 73c10f2
Bring better docs.
wkoszek 797dce4
Back to flags.
wkoszek ff8dcdd
Fix the docs.
wkoszek 391a32e
Format.
wkoszek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # MCP HTTP Example | ||
|
|
||
| This example demonstrates how to use the Model Context Protocol (MCP) over HTTP using the streamable transport. It includes both a server and client implementation. | ||
|
|
||
| ## Overview | ||
|
|
||
| The example implements: | ||
| - A server that provides a `get_time` tool | ||
| - A client that connects to the server, lists available tools, and calls the `get_time` tool | ||
|
|
||
| ## Running the Example | ||
|
|
||
| When run with -server, the program becomes an MCP server. | ||
| When run with -client, it becomes an MCP client. | ||
| Server and client support passing custom -port and -host. | ||
|
|
||
| ### Start the Server | ||
|
|
||
| ```bash | ||
| go run main.go -server | ||
| ``` | ||
|
|
||
| This starts an MCP server on `localhost:8080` (default) that provides a `get_time` tool. | ||
|
|
||
| ### Run the Client | ||
|
|
||
| In another terminal: | ||
|
|
||
| ```bash | ||
| go run main.go -client | ||
| ``` | ||
|
|
||
| The client will: | ||
| 1. Connect to the server | ||
| 2. List available tools | ||
| 3. Call the `get_time` tool for NYC, San Francisco, and Boston | ||
| 4. Display the results | ||
|
|
||
|
|
||
| ## Testing with real-world MCP Clients | ||
|
|
||
| Once the server is started, assuming it's the default | ||
| localhost:8080, you can try to add it to a popular MCP client: | ||
|
|
||
| claude mcp add -t http timezone http://localhost:8080 | ||
|
|
||
| Once added, Claude Code will be able to discover and use the `get_time` tool provided by this server. | ||
|
|
||
| In Claude Code: | ||
|
|
||
| > what's the timezone | ||
|
|
||
| ⏺ I'll get the current time in a major US city for you. | ||
|
|
||
| ⏺ timezone - get_time (MCP)(city: "nyc") | ||
| ⎿ The current time in New York City is 7:30:16 PM EDT on Wedn | ||
| esday, July 23, 2025 | ||
|
|
||
|
|
||
| ⏺ The current timezone is EDT (Eastern Daylight Time), and it's | ||
| 7:30 PM on Wednesday, July 23, 2025. | ||
|
|
||
| > what timezones do you support? | ||
|
|
||
| ⏺ The timezone tool supports three US cities: | ||
| - NYC (Eastern Time) | ||
| - SF (Pacific Time) | ||
| - Boston (Eastern Time) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| // Copyright 2025 The Go MCP SDK Authors. All rights reserved. | ||
| // Use of this source code is governed by an MIT-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "net/http" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
| ) | ||
|
|
||
| func main() { | ||
| var ( | ||
| serverMode = flag.Bool("server", false, "Run as server") | ||
| clientMode = flag.Bool("client", false, "Run as client") | ||
| host = flag.String("host", "localhost", "Host to connect to or listen on") | ||
| port = flag.String("port", "8080", "Port to connect to or listen on") | ||
| ) | ||
|
|
||
| flag.Usage = func() { | ||
| fmt.Fprintf(os.Stderr, "Usage: %s [options]\n\n", os.Args[0]) | ||
| fmt.Fprintf(os.Stderr, "This program demonstrates MCP over HTTP using the streamable transport.\n") | ||
| fmt.Fprintf(os.Stderr, "It can run as either a server or client.\n\n") | ||
| fmt.Fprintf(os.Stderr, "Options:\n") | ||
| flag.PrintDefaults() | ||
| fmt.Fprintf(os.Stderr, "\nExamples:\n") | ||
| fmt.Fprintf(os.Stderr, " Run as server: %s -server\n", os.Args[0]) | ||
| fmt.Fprintf(os.Stderr, " Run as client: %s -client\n", os.Args[0]) | ||
| fmt.Fprintf(os.Stderr, " Custom host/port: %s -server -host 0.0.0.0 -port 9090\n", os.Args[0]) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| flag.Parse() | ||
|
|
||
| if (*serverMode && *clientMode) || (!*serverMode && !*clientMode) { | ||
wkoszek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fmt.Fprintf(os.Stderr, "Error: Must specify exactly one of -server or -client\n\n") | ||
| flag.Usage() | ||
| } | ||
|
|
||
| if *serverMode { | ||
| runServer(*host, *port) | ||
| } else { | ||
| runClient(*host, *port) | ||
| } | ||
| } | ||
|
|
||
| // GetTimeParams defines the parameters for the get_time tool | ||
| type GetTimeParams struct { | ||
| City string `json:"city" jsonschema:"City to get time for (nyc, sf, or boston)"` | ||
| } | ||
|
|
||
| // getTime implements the tool that returns the current time for a given city | ||
| func getTime(ctx context.Context, ss *mcp.ServerSession, params *mcp.CallToolParamsFor[GetTimeParams]) (*mcp.CallToolResultFor[any], error) { | ||
| // Define time zones for each city | ||
| locations := map[string]string{ | ||
| "nyc": "America/New_York", | ||
| "sf": "America/Los_Angeles", | ||
| "boston": "America/New_York", | ||
| } | ||
|
|
||
| city := params.Arguments.City | ||
| if city == "" { | ||
| city = "nyc" // Default to NYC | ||
| } | ||
|
|
||
| // Get the timezone | ||
| tzName, ok := locations[city] | ||
| if !ok { | ||
| return nil, fmt.Errorf("unknown city: %s", city) | ||
| } | ||
|
|
||
| // Load the location | ||
| loc, err := time.LoadLocation(tzName) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to load timezone: %w", err) | ||
| } | ||
|
|
||
| // Get current time in that location | ||
| now := time.Now().In(loc) | ||
|
|
||
| // Format the response | ||
| cityNames := map[string]string{ | ||
| "nyc": "New York City", | ||
| "sf": "San Francisco", | ||
| "boston": "Boston", | ||
| } | ||
|
|
||
| response := fmt.Sprintf("The current time in %s is %s", | ||
| cityNames[city], | ||
| now.Format("3:04:05 PM MST on Monday, January 2, 2006")) | ||
wkoszek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return &mcp.CallToolResultFor[any]{ | ||
| Content: []mcp.Content{ | ||
| &mcp.TextContent{Text: response}, | ||
| }, | ||
| }, nil | ||
| } | ||
|
|
||
| // responseWriter wraps http.ResponseWriter to capture the status code | ||
| type responseWriter struct { | ||
| http.ResponseWriter | ||
| statusCode int | ||
| } | ||
|
|
||
| func (rw *responseWriter) WriteHeader(code int) { | ||
| rw.statusCode = code | ||
| rw.ResponseWriter.WriteHeader(code) | ||
| } | ||
|
|
||
| func runServer(host, port string) { | ||
| // Create an MCP server | ||
| server := mcp.NewServer(&mcp.Implementation{ | ||
| Name: "time-server", | ||
| Version: "1.0.0", | ||
| }, nil) | ||
|
|
||
| // Add the get_time tool | ||
| mcp.AddTool(server, &mcp.Tool{ | ||
| Name: "get_time", | ||
| Description: "Get the current time in NYC, San Francisco, or Boston", | ||
| }, getTime) | ||
|
|
||
| // Create the streamable HTTP handler | ||
| handler := mcp.NewStreamableHTTPHandler(func(req *http.Request) *mcp.Server { | ||
| return server | ||
| }, nil) | ||
|
|
||
| // Wrap the handler with logging middleware | ||
| loggingHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
wkoszek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| start := time.Now() | ||
|
|
||
| // Create a response writer wrapper to capture status code | ||
| wrapped := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK} | ||
|
|
||
| // Log request details | ||
| log.Printf("[REQUEST] %s | %s | %s %s", | ||
| start.Format("2006-01-02 15:04:05.000"), | ||
| r.RemoteAddr, | ||
| r.Method, | ||
| r.URL.Path) | ||
|
|
||
| // Call the actual handler | ||
| handler.ServeHTTP(wrapped, r) | ||
|
|
||
| // Log response details | ||
| duration := time.Since(start) | ||
| log.Printf("[RESPONSE] %s | %s | %s %s | Status: %d | Duration: %v", | ||
| time.Now().Format("2006-01-02 15:04:05.000"), | ||
| r.RemoteAddr, | ||
| r.Method, | ||
| r.URL.Path, | ||
| wrapped.statusCode, | ||
| duration) | ||
| }) | ||
|
|
||
| addr := fmt.Sprintf("%s:%s", host, port) | ||
| log.Printf("MCP server listening on http://%s", addr) | ||
| log.Printf("Available tool: get_time (cities: nyc, sf, boston)") | ||
|
|
||
| // Start the HTTP server with logging handler | ||
| if err := http.ListenAndServe(addr, loggingHandler); err != nil { | ||
| log.Fatalf("Server failed: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func runClient(host, port string) { | ||
| ctx := context.Background() | ||
|
|
||
| // Create the URL for the server | ||
| url := fmt.Sprintf("http://%s:%s", host, port) | ||
| log.Printf("Connecting to MCP server at %s", url) | ||
|
|
||
| // Create a streamable client transport | ||
| transport := mcp.NewStreamableClientTransport(url, nil) | ||
|
|
||
| // Create an MCP client | ||
| client := mcp.NewClient(&mcp.Implementation{ | ||
| Name: "time-client", | ||
| Version: "1.0.0", | ||
| }, nil) | ||
|
|
||
| // Connect to the server | ||
| session, err := client.Connect(ctx, transport) | ||
| if err != nil { | ||
| log.Fatalf("Failed to connect: %v", err) | ||
| } | ||
| defer session.Close() | ||
|
|
||
| log.Printf("Connected to server (session ID: %s)", session.ID()) | ||
|
|
||
| // First, list available tools | ||
| log.Println("\nListing available tools...") | ||
wkoszek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| toolsResult, err := session.ListTools(ctx, &mcp.ListToolsParams{}) | ||
wkoszek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| log.Fatalf("Failed to list tools: %v", err) | ||
| } | ||
|
|
||
| for _, tool := range toolsResult.Tools { | ||
| log.Printf(" - %s: %s", tool.Name, tool.Description) | ||
| } | ||
|
|
||
| // Call the get_time tool for each city | ||
| cities := []string{"nyc", "sf", "boston"} | ||
|
|
||
| log.Println("\nGetting time for each city...") | ||
| for _, city := range cities { | ||
| // Call the tool | ||
| result, err := session.CallTool(ctx, &mcp.CallToolParams{ | ||
| Name: "get_time", | ||
| Arguments: map[string]interface{}{ | ||
wkoszek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "city": city, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| log.Printf("Failed to get time for %s: %v", city, err) | ||
| continue | ||
| } | ||
|
|
||
| // Print the result | ||
| for _, content := range result.Content { | ||
| if textContent, ok := content.(*mcp.TextContent); ok { | ||
| log.Printf(" %s", textContent.Text) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| log.Println("\nClient completed successfully") | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.