-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: support in-process fastmcp server #546
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,14 @@ | ||
# MCP In-Process Example | ||
|
||
This example uses an in-process fastmcp server in [server.py](server.py). | ||
|
||
Run the example via: | ||
|
||
``` | ||
uv run python -m examples.mcp.in_process.main | ||
``` | ||
|
||
## Details | ||
|
||
The example uses the `MCPServerSse` class from `agents.mcp`. The server runs in a sub-process at `https://localhost:8000/sse`. | ||
|
Empty file.
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,52 @@ | ||
import asyncio | ||
import os | ||
import shutil | ||
import subprocess | ||
import time | ||
from typing import Any | ||
|
||
from agents import Agent, Runner, gen_trace_id, trace | ||
from agents.mcp import MCPServer | ||
from agents.mcp.fastmcp import FastMCPServer | ||
from agents.model_settings import ModelSettings | ||
|
||
from .server import mcp | ||
|
||
|
||
async def run(mcp_server: MCPServer): | ||
agent = Agent( | ||
name="Assistant", | ||
instructions="Use the tools to answer the questions.", | ||
mcp_servers=[mcp_server], | ||
model_settings=ModelSettings(tool_choice="required"), | ||
) | ||
|
||
# Use the `add` tool to add two numbers | ||
message = "Add these numbers: 7 and 22." | ||
print(f"Running: {message}") | ||
result = await Runner.run(starting_agent=agent, input=message) | ||
print(result.final_output) | ||
|
||
# Run the `get_weather` tool | ||
message = "What's the weather in Tokyo?" | ||
print(f"\n\nRunning: {message}") | ||
result = await Runner.run(starting_agent=agent, input=message) | ||
print(result.final_output) | ||
|
||
# Run the `get_secret_word` tool | ||
message = "What's the secret word?" | ||
print(f"\n\nRunning: {message}") | ||
result = await Runner.run(starting_agent=agent, input=message) | ||
print(result.final_output) | ||
|
||
|
||
async def main(): | ||
async with FastMCPServer(mcp) as server: | ||
trace_id = gen_trace_id() | ||
with trace(workflow_name="In-Process Example", trace_id=trace_id): | ||
print(f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}\n") | ||
await run(server) | ||
|
||
|
||
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,29 @@ | ||
import random | ||
|
||
import requests | ||
from fastmcp.server import FastMCP | ||
|
||
# Create server | ||
mcp = FastMCP("Echo Server") | ||
|
||
|
||
@mcp.tool() | ||
def add(a: int, b: int) -> int: | ||
"""Add two numbers""" | ||
print(f"[debug-server] add({a}, {b})") | ||
return a + b | ||
|
||
|
||
@mcp.tool() | ||
def get_secret_word() -> str: | ||
print("[debug-server] get_secret_word()") | ||
return random.choice(["apple", "banana", "cherry"]) | ||
|
||
|
||
@mcp.tool() | ||
def get_current_weather(city: str) -> str: | ||
print(f"[debug-server] get_current_weather({city})") | ||
|
||
endpoint = "https://wttr.in" | ||
response = requests.get(f"{endpoint}/{city}") | ||
return response.text |
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ name = "openai-agents" | |
version = "0.0.11" | ||
description = "OpenAI Agents SDK" | ||
readme = "README.md" | ||
requires-python = ">=3.9" | ||
requires-python = ">=3.10" | ||
license = "MIT" | ||
authors = [{ name = "OpenAI", email = "[email protected]" }] | ||
dependencies = [ | ||
|
@@ -14,12 +14,12 @@ dependencies = [ | |
"requests>=2.0, <3", | ||
"types-requests>=2.0, <3", | ||
"mcp>=1.6.0, <2; python_version >= '3.10'", | ||
"fastmcp>=2.2.0" | ||
] | ||
classifiers = [ | ||
"Typing :: Typed", | ||
"Intended Audience :: Developers", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3.9", | ||
"Programming Language :: Python :: 3.10", | ||
"Programming Language :: Python :: 3.11", | ||
"Programming Language :: Python :: 3.12", | ||
|
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,55 @@ | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
from agents.mcp import MCPServer | ||
from fastmcp.client import Client, ClientTransport | ||
from fastmcp.server import FastMCP | ||
from pydantic import AnyUrl | ||
|
||
from mcp.types import CallToolResult, Tool | ||
|
||
|
||
class FastMCPServer(MCPServer): | ||
""" | ||
Support fastmcp transport implementations, include in-memory fastmcp servers. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
transport: ClientTransport | FastMCP | AnyUrl | Path | str, | ||
name: str | None = None, | ||
): | ||
self._client = Client(transport) | ||
if not name: | ||
if isinstance(transport, FastMCP): | ||
name = transport.name | ||
else: | ||
name = str(transport) | ||
self._name = name | ||
|
||
async def connect(self): | ||
await self._client.__aenter__() | ||
|
||
async def cleanup(self): | ||
await self._client.__aexit__(None, None, None) | ||
|
||
async def __aenter__(self): | ||
await self.connect() | ||
return self | ||
|
||
async def __aexit__(self, exc_type, exc_value, traceback): | ||
await self._client.__aexit__(exc_type, exc_value, traceback) | ||
|
||
@property | ||
def name(self) -> str: | ||
return self._name | ||
|
||
async def list_tools(self) -> list[Tool]: | ||
return await self._client.list_tools() | ||
|
||
async def call_tool( | ||
self, tool_name: str, arguments: dict[str, Any] | None | ||
) -> CallToolResult: | ||
return await self._client.call_tool( | ||
tool_name, arguments, _return_raw_result=True | ||
) |
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.
Uh oh!
There was an error while loading. Please reload this page.