Spaces:
Running
Running
Commit ·
4e8b154
1
Parent(s): 54354ec
Fix remark to use the fastmcp Client.
Browse files- tests/test_examples.py +39 -46
tests/test_examples.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 1 |
"""Tests for example servers"""
|
| 2 |
|
| 3 |
import pytest
|
| 4 |
-
from mcp.shared.memory import (
|
| 5 |
-
create_connected_server_and_client_session as client_session,
|
| 6 |
-
)
|
| 7 |
from mcp.types import (
|
| 8 |
PromptMessage,
|
| 9 |
TextContent,
|
|
@@ -11,18 +8,19 @@ from mcp.types import (
|
|
| 11 |
)
|
| 12 |
from pydantic import AnyUrl
|
| 13 |
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@pytest.mark.anyio
|
| 16 |
async def test_simple_echo():
|
| 17 |
"""Test the simple echo server"""
|
| 18 |
from examples.simple_echo import mcp
|
| 19 |
|
| 20 |
-
async with
|
| 21 |
result = await client.call_tool("echo", {"text": "hello"})
|
| 22 |
-
assert len(result
|
| 23 |
-
|
| 24 |
-
assert
|
| 25 |
-
assert content.text == "hello"
|
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.anyio
|
|
@@ -30,14 +28,14 @@ async def test_complex_inputs():
|
|
| 30 |
"""Test the complex inputs server"""
|
| 31 |
from examples.complex_inputs import mcp
|
| 32 |
|
| 33 |
-
async with
|
| 34 |
tank = {"shrimp": [{"name": "bob"}, {"name": "alice"}]}
|
| 35 |
result = await client.call_tool(
|
| 36 |
"name_shrimp", {"tank": tank, "extra_names": ["charlie"]}
|
| 37 |
)
|
| 38 |
-
assert len(result
|
| 39 |
-
assert isinstance(result
|
| 40 |
-
assert result
|
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.anyio
|
|
@@ -45,21 +43,19 @@ async def test_desktop(monkeypatch):
|
|
| 45 |
"""Test the desktop server"""
|
| 46 |
from examples.desktop import mcp
|
| 47 |
|
| 48 |
-
async with
|
| 49 |
# Test the add function
|
| 50 |
result = await client.call_tool("add", {"a": 1, "b": 2})
|
| 51 |
-
assert len(result
|
| 52 |
-
|
| 53 |
-
assert
|
| 54 |
-
assert content.text == "3"
|
| 55 |
|
| 56 |
-
async with
|
| 57 |
result = await client.read_resource(AnyUrl("greeting://rooter12"))
|
| 58 |
-
assert len(result
|
| 59 |
-
|
| 60 |
-
assert isinstance(
|
| 61 |
-
assert
|
| 62 |
-
assert content.text == "Hello, rooter12!"
|
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.anyio
|
|
@@ -67,33 +63,30 @@ async def test_echo():
|
|
| 67 |
"""Test the echo server"""
|
| 68 |
from examples.echo import mcp
|
| 69 |
|
| 70 |
-
async with
|
| 71 |
result = await client.call_tool("echo_tool", {"text": "hello"})
|
| 72 |
-
assert len(result
|
| 73 |
-
|
| 74 |
-
assert
|
| 75 |
-
assert content.text == "hello"
|
| 76 |
|
| 77 |
-
async with
|
| 78 |
result = await client.read_resource(AnyUrl("echo://static"))
|
| 79 |
-
assert len(result
|
| 80 |
-
|
| 81 |
-
assert isinstance(
|
| 82 |
-
assert
|
| 83 |
-
assert content.text == "Echo!"
|
| 84 |
|
| 85 |
-
async with
|
| 86 |
result = await client.read_resource(AnyUrl("echo://server42"))
|
| 87 |
-
assert len(result
|
| 88 |
-
|
| 89 |
-
assert isinstance(
|
| 90 |
-
assert
|
| 91 |
-
assert content.text == "Echo: server42"
|
| 92 |
|
| 93 |
-
async with
|
| 94 |
result = await client.get_prompt("echo", {"text": "hello"})
|
| 95 |
-
assert len(result
|
| 96 |
-
assert isinstance(
|
| 97 |
-
assert isinstance(result
|
| 98 |
-
assert isinstance(result
|
| 99 |
-
assert result
|
|
|
|
| 1 |
"""Tests for example servers"""
|
| 2 |
|
| 3 |
import pytest
|
|
|
|
|
|
|
|
|
|
| 4 |
from mcp.types import (
|
| 5 |
PromptMessage,
|
| 6 |
TextContent,
|
|
|
|
| 8 |
)
|
| 9 |
from pydantic import AnyUrl
|
| 10 |
|
| 11 |
+
from fastmcp import Client
|
| 12 |
+
|
| 13 |
|
| 14 |
@pytest.mark.anyio
|
| 15 |
async def test_simple_echo():
|
| 16 |
"""Test the simple echo server"""
|
| 17 |
from examples.simple_echo import mcp
|
| 18 |
|
| 19 |
+
async with Client(mcp) as client:
|
| 20 |
result = await client.call_tool("echo", {"text": "hello"})
|
| 21 |
+
assert len(result) == 1
|
| 22 |
+
assert isinstance(result[0], TextContent)
|
| 23 |
+
assert result[0].text == "hello"
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.anyio
|
|
|
|
| 28 |
"""Test the complex inputs server"""
|
| 29 |
from examples.complex_inputs import mcp
|
| 30 |
|
| 31 |
+
async with Client(mcp) as client:
|
| 32 |
tank = {"shrimp": [{"name": "bob"}, {"name": "alice"}]}
|
| 33 |
result = await client.call_tool(
|
| 34 |
"name_shrimp", {"tank": tank, "extra_names": ["charlie"]}
|
| 35 |
)
|
| 36 |
+
assert len(result) == 1
|
| 37 |
+
assert isinstance(result[0], TextContent)
|
| 38 |
+
assert result[0].text == '[\n "bob",\n "alice",\n "charlie"\n]'
|
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.anyio
|
|
|
|
| 43 |
"""Test the desktop server"""
|
| 44 |
from examples.desktop import mcp
|
| 45 |
|
| 46 |
+
async with Client(mcp) as client:
|
| 47 |
# Test the add function
|
| 48 |
result = await client.call_tool("add", {"a": 1, "b": 2})
|
| 49 |
+
assert len(result) == 1
|
| 50 |
+
assert isinstance(result[0], TextContent)
|
| 51 |
+
assert result[0].text == "3"
|
|
|
|
| 52 |
|
| 53 |
+
async with Client(mcp) as client:
|
| 54 |
result = await client.read_resource(AnyUrl("greeting://rooter12"))
|
| 55 |
+
assert len(result) == 1
|
| 56 |
+
assert isinstance(result[0], TextResourceContents)
|
| 57 |
+
assert isinstance(result[0].text, str)
|
| 58 |
+
assert result[0].text == "Hello, rooter12!"
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
@pytest.mark.anyio
|
|
|
|
| 63 |
"""Test the echo server"""
|
| 64 |
from examples.echo import mcp
|
| 65 |
|
| 66 |
+
async with Client(mcp) as client:
|
| 67 |
result = await client.call_tool("echo_tool", {"text": "hello"})
|
| 68 |
+
assert len(result) == 1
|
| 69 |
+
assert isinstance(result[0], TextContent)
|
| 70 |
+
assert result[0].text == "hello"
|
|
|
|
| 71 |
|
| 72 |
+
async with Client(mcp) as client:
|
| 73 |
result = await client.read_resource(AnyUrl("echo://static"))
|
| 74 |
+
assert len(result) == 1
|
| 75 |
+
assert isinstance(result[0], TextResourceContents)
|
| 76 |
+
assert isinstance(result[0].text, str)
|
| 77 |
+
assert result[0].text == "Echo!"
|
|
|
|
| 78 |
|
| 79 |
+
async with Client(mcp) as client:
|
| 80 |
result = await client.read_resource(AnyUrl("echo://server42"))
|
| 81 |
+
assert len(result) == 1
|
| 82 |
+
assert isinstance(result[0], TextResourceContents)
|
| 83 |
+
assert isinstance(result[0].text, str)
|
| 84 |
+
assert result[0].text == "Echo: server42"
|
|
|
|
| 85 |
|
| 86 |
+
async with Client(mcp) as client:
|
| 87 |
result = await client.get_prompt("echo", {"text": "hello"})
|
| 88 |
+
assert len(result) == 1
|
| 89 |
+
assert isinstance(result[0], PromptMessage)
|
| 90 |
+
assert isinstance(result[0].content, TextContent)
|
| 91 |
+
assert isinstance(result[0].content.text, str)
|
| 92 |
+
assert result[0].content.text == "hello"
|