go-sdk/examples/http
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Spring_MT 35dfcf21c9
examples: Fix SSE streaming issue by moving logging from HTTP to MCP middlewarre (#614)
In `examples/http`, previous HTTP-level logging middleware blocked SSE
streaming because `handler.ServeHTTP()` does not return until the
connection closes for GET requests (SSE streams).
This prevented response headers from being sent to the client.

Changes:
- Replace HTTP-level logging handler with MCP middleware pattern
- Implement logging at the MCP protocol layer using mcp.Middleware
- Add middleware to server using AddReceivingMiddleware()
- Simplify main.go by removing HTTP handler wrapping

This allows SSE streams to work properly while still providing detailed
logging for all MCP protocol-level method calls.

Fixes #632
2025-11-11 11:56:52 -05:00
..
logging_middleware.go examples: Fix SSE streaming issue by moving logging from HTTP to MCP middlewarre (#614) 2025-11-11 11:56:52 -05:00
main.go examples: Fix SSE streaming issue by moving logging from HTTP to MCP middlewarre (#614) 2025-11-11 11:56:52 -05:00
README.md docs: improve documentation clarity and client integration steps (#567) 2025-10-13 11:47:58 -04:00

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 cityTime tool
  • A client that connects to the server, lists available tools, and calls the cityTime tool

Usage

Start the Server

go run . server

This starts an MCP server on http://localhost:8080 (default) that provides a cityTime tool.

To run a client in another terminal:

go run . client

The client will:

  1. Connect to the server
  2. List available tools
  3. Call the cityTime tool for NYC, San Francisco, and Boston
  4. Display the results

At any given time you can pass a custom URL to the program to run it on a custom host/port:

go run . -host 0.0.0.0 -port 9000 server

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 cityTime 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 - cityTime (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)