mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-19 20:14:17 +02:00
Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54a3c4a8f6 |
4 changed files with 1012 additions and 2 deletions
21
examples/chat_with_server.py
Normal file
21
examples/chat_with_server.py
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
from fastmcp.testing import TestClient
|
||||||
|
|
||||||
|
server = FastMCP("test-server")
|
||||||
|
|
||||||
|
|
||||||
|
@server.tool()
|
||||||
|
def get_the_value_of_schleeb() -> int:
|
||||||
|
return 42
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
async with TestClient(server) as client:
|
||||||
|
await client.say("What is the value of schleeb?")
|
||||||
|
await client.say("sorry can you repeat that?")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
|
|
@ -36,6 +36,9 @@ classifiers = [
|
||||||
"Typing :: Typed",
|
"Typing :: Typed",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
chat = ["marvin>=3.1.0"]
|
||||||
|
|
||||||
[dependency-groups]
|
[dependency-groups]
|
||||||
dev = [
|
dev = [
|
||||||
"copychat>=0.5.2",
|
"copychat>=0.5.2",
|
||||||
|
|
|
||||||
37
src/fastmcp/testing.py
Normal file
37
src/fastmcp/testing.py
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
from contextlib import ExitStack
|
||||||
|
from types import ModuleType
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from typing_extensions import Self
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
|
||||||
|
def _get_agent_framework_module() -> ModuleType:
|
||||||
|
try:
|
||||||
|
import marvin
|
||||||
|
except ImportError:
|
||||||
|
raise ImportError(
|
||||||
|
"please install `fastmcp[chat]` to use TestClient with chat tools"
|
||||||
|
)
|
||||||
|
return marvin
|
||||||
|
|
||||||
|
|
||||||
|
class TestClient:
|
||||||
|
def __init__(self, server: FastMCP, agent_options: dict[str, Any] | None = None):
|
||||||
|
self.server = server
|
||||||
|
self._agent_framework = _get_agent_framework_module()
|
||||||
|
self._agent_options = agent_options or {}
|
||||||
|
self._stack = ExitStack()
|
||||||
|
|
||||||
|
async def __aenter__(self) -> Self:
|
||||||
|
self._stack.enter_context(self._agent_framework.Thread())
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type, exc_value, traceback):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def say(self, message: str) -> None:
|
||||||
|
await self._agent_framework.Agent(
|
||||||
|
mcp_servers=[self.server], **self._agent_options
|
||||||
|
).run_async(message)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue