MCP ExplorerExplorer

Mcp Streamablehttp Sample

@moritalouson a year ago
1 MIT
FreeCommunity
AI Systems
Sample code for Streamable HTTP implementation of MCP, supporting stateful communication.

Overview

What is Mcp Streamablehttp Sample

mcp-streamablehttp-sample is a sample code repository that demonstrates the Streamable HTTP implementation of the Model Context Protocol (MCP), which facilitates communication between clients and servers using various methods.

Use cases

Use cases include building interactive web applications that utilize Server-Sent Events for real-time updates, implementing tools that require stateful communication, and developing applications that need to manage multiple client connections efficiently.

How to use

To use mcp-streamablehttp-sample, clone the repository, install dependencies using npm or yarn, and set up the TypeScript SDK. Start the server and client using the provided npm scripts, adjusting the port and server URL as needed.

Key features

Key features include server-side implementation of Streamable HTTP transport, support for multiple client connections, session management, and client-side capabilities for connecting to the server and invoking tools.

Where to use

mcp-streamablehttp-sample can be used in web applications that require real-time communication between clients and servers, such as chat applications, live data feeds, and collaborative tools.

Content

MCP Streamable HTTP Sample

This repository provides sample code for the Streamable HTTP implementation of the Model Context Protocol (MCP).

Overview

MCP (Model Context Protocol) is a communication protocol between clients and servers that supports multiple communication methods. This sample demonstrates an implementation using the Streamable HTTP method.

Streamable HTTP has the following characteristics:

  • Sending messages via HTTP POST requests
  • Establishing Server-Sent Events (SSE) streams via HTTP GET requests
  • Stateful communication through session management

Features

This sample includes the following features:

  • Server-side:

    • Implementation of Streamable HTTP transport
    • Support for multiple client connections
    • Provision of tool functions (echo, add)
    • Session management
  • Client-side:

    • Connection to the server
    • Retrieving tool lists
    • Invoking tools

Prerequisites

  • Node.js 18.x or higher
  • npm or yarn

Installation

# Clone the repository (including submodules)
git clone --recursive https://github.com/moritalous/mcp-streamablehttp-sample.git
cd mcp-streamablehttp-sample

# Install dependencies
npm install

# Set up TypeScript SDK
npm run setup

Build

npm run build

How to Run

Starting the Server

npm run start:server

The server starts on port 3001 by default. You can change the port number by setting the PORT environment variable.

Running the Client

npm run start:client

The client connects to http://localhost:3001/mcp by default. You can change the connection destination by setting the MCP_SERVER_URL environment variable.

Development

Running in development mode:

# Server (development mode)
npm run dev:server

# Client (development mode)
npm run dev:client

Project Structure

mcp-streamablehttp-sample/
├── src/
│   ├── client.ts     # Client implementation
│   └── server.ts     # Server implementation
├── typescript-sdk/   # MCP TypeScript SDK (Git submodule)
├── dist/             # Compiled code
├── package.json      # Project configuration
└── tsconfig.json     # TypeScript configuration

License

MIT

References

Note: A Japanese version of this README is available at README_ja.md

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers