-
Notifications
You must be signed in to change notification settings - Fork 2.1k
README - replace code snippets with examples - streamable http #1155
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
+186
−73
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d6a51f7
add lowlevel to snippets
ihrpr cb85e20
streamable http examples
ihrpr cb1a316
pyright
ihrpr 925b22f
revert
ihrpr 362b00e
use starlette
ihrpr e3ebab9
Merge branch 'main' into ihrpr/remote-snippets
ihrpr d87f26d
remove docstring
ihrpr 50d1248
add type
ihrpr 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
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
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
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
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,29 @@ | ||
""" | ||
Run from the repository root: | ||
uv run examples/snippets/clients/streamable_basic.py | ||
""" | ||
|
||
import asyncio | ||
|
||
from mcp import ClientSession | ||
from mcp.client.streamable_http import streamablehttp_client | ||
|
||
|
||
async def main(): | ||
# Connect to a streamable HTTP server | ||
async with streamablehttp_client("http://localhost:8000/mcp") as ( | ||
read_stream, | ||
write_stream, | ||
_, | ||
): | ||
# Create a session using the client streams | ||
async with ClientSession(read_stream, write_stream) as session: | ||
# Initialize the connection | ||
await session.initialize() | ||
# List available tools | ||
tools = await session.list_tools() | ||
print(f"Available tools: {[tool.name for tool in tools.tools]}") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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,28 @@ | ||
""" | ||
Run from the repository root: | ||
uv run examples/snippets/servers/streamable_config.py | ||
""" | ||
|
||
from mcp.server.fastmcp import FastMCP | ||
|
||
# Stateful server (maintains session state) | ||
mcp = FastMCP("StatefulServer") | ||
|
||
# Other configuration options: | ||
# Stateless server (no session persistence) | ||
# mcp = FastMCP("StatelessServer", stateless_http=True) | ||
|
||
# Stateless server (no session persistence, no sse stream with supported client) | ||
# mcp = FastMCP("StatelessServer", stateless_http=True, json_response=True) | ||
|
||
|
||
# Add a simple tool to demonstrate the server | ||
@mcp.tool() | ||
def greet(name: str = "World") -> str: | ||
"""Greet someone by name.""" | ||
return f"Hello, {name}!" | ||
|
||
|
||
# Run server with streamable_http transport | ||
if __name__ == "__main__": | ||
mcp.run(transport="streamable-http") |
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,49 @@ | ||
""" | ||
Run from the repository root: | ||
uvicorn examples.snippets.servers.streamable_starlette_mount:app --reload | ||
""" | ||
|
||
import contextlib | ||
|
||
from starlette.applications import Starlette | ||
from starlette.routing import Mount | ||
|
||
from mcp.server.fastmcp import FastMCP | ||
|
||
# Create the Echo server | ||
echo_mcp = FastMCP(name="EchoServer", stateless_http=True) | ||
|
||
|
||
@echo_mcp.tool() | ||
def echo(message: str) -> str: | ||
"""A simple echo tool""" | ||
return f"Echo: {message}" | ||
|
||
|
||
# Create the Math server | ||
math_mcp = FastMCP(name="MathServer", stateless_http=True) | ||
|
||
|
||
@math_mcp.tool() | ||
def add_two(n: int) -> int: | ||
"""Tool to add two to the input""" | ||
return n + 2 | ||
|
||
|
||
# Create a combined lifespan to manage both session managers | ||
@contextlib.asynccontextmanager | ||
async def lifespan(app): | ||
async with contextlib.AsyncExitStack() as stack: | ||
await stack.enter_async_context(echo_mcp.session_manager.run()) | ||
await stack.enter_async_context(math_mcp.session_manager.run()) | ||
yield | ||
|
||
|
||
# Create the Starlette app and mount the MCP servers | ||
app = Starlette( | ||
routes=[ | ||
Mount("/echo", echo_mcp.streamable_http_app()), | ||
Mount("/math", math_mcp.streamable_http_app()), | ||
], | ||
lifespan=lifespan, | ||
) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.