Skip to content

Commit 9419e5c

Browse files
Nikita BelliniNikita Bellini
authored andcommitted
Merge branch 'main' into feature/runtime-tools
2 parents e9f7650 + 906ceea commit 9419e5c

File tree

10 files changed

+553
-500
lines changed

10 files changed

+553
-500
lines changed

.github/workflows/shared.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
test:
3030
runs-on: ${{ matrix.os }}
3131
timeout-minutes: 10
32+
continue-on-error: true
3233
strategy:
3334
matrix:
3435
python-version: ["3.10", "3.11", "3.12", "3.13"]
@@ -48,8 +49,14 @@ jobs:
4849

4950
- name: Run pytest
5051
run: uv run --frozen --no-sync pytest
51-
continue-on-error: true
5252

53+
# This must run last as it modifies the environment!
54+
- name: Run pytest with lowest versions
55+
run: |
56+
uv sync --all-extras --upgrade
57+
uv run --no-sync pytest
58+
env:
59+
UV_RESOLUTION: lowest-direct
5360
readme-snippets:
5461
runs-on: ubuntu-latest
5562
steps:

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,13 +1366,13 @@ The MCP protocol defines three core primitives that servers can implement:
13661366

13671367
MCP servers declare capabilities during initialization:
13681368

1369-
| Capability | Feature Flag | Description |
1370-
|-------------|------------------------------|------------------------------------|
1371-
| `prompts` | `listChanged` | Prompt template management |
1372-
| `resources` | `subscribe`<br/>`listChanged`| Resource exposure and updates |
1373-
| `tools` | `listChanged` | Tool discovery and execution |
1374-
| `logging` | - | Server logging configuration |
1375-
| `completion`| - | Argument completion suggestions |
1369+
| Capability | Feature Flag | Description |
1370+
|--------------|------------------------------|------------------------------------|
1371+
| `prompts` | `listChanged` | Prompt template management |
1372+
| `resources` | `subscribe`<br/>`listChanged`| Resource exposure and updates |
1373+
| `tools` | `listChanged` | Tool discovery and execution |
1374+
| `logging` | - | Server logging configuration |
1375+
| `completions`| - | Argument completion suggestions |
13761376

13771377
## Documentation
13781378

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies = [
2525
"anyio>=4.5",
2626
"httpx>=0.27",
2727
"httpx-sse>=0.4",
28-
"pydantic>=2.7.2,<3.0.0",
28+
"pydantic>=2.8.0,<3.0.0",
2929
"starlette>=0.27",
3030
"python-multipart>=0.0.9",
3131
"sse-starlette>=1.6.1",
@@ -36,14 +36,13 @@ dependencies = [
3636

3737
[project.optional-dependencies]
3838
rich = ["rich>=13.9.4"]
39-
cli = ["typer>=0.12.4", "python-dotenv>=1.0.0"]
39+
cli = ["typer>=0.16.0", "python-dotenv>=1.0.0"]
4040
ws = ["websockets>=15.0.1"]
4141

4242
[project.scripts]
4343
mcp = "mcp.cli:app [cli]"
4444

4545
[tool.uv]
46-
resolution = "lowest-direct"
4746
default-groups = ["dev", "docs"]
4847
required-version = ">=0.7.2"
4948

@@ -58,6 +57,7 @@ dev = [
5857
"pytest-examples>=0.0.14",
5958
"pytest-pretty>=1.2.0",
6059
"inline-snapshot>=0.23.0",
60+
"dirty-equals>=0.9.0",
6161
]
6262
docs = [
6363
"mkdocs>=1.6.1",
@@ -124,5 +124,5 @@ filterwarnings = [
124124
# This should be fixed on Uvicorn's side.
125125
"ignore::DeprecationWarning:websockets",
126126
"ignore:websockets.server.WebSocketServerProtocol is deprecated:DeprecationWarning",
127-
"ignore:Returning str or bytes.*:DeprecationWarning:mcp.server.lowlevel"
127+
"ignore:Returning str or bytes.*:DeprecationWarning:mcp.server.lowlevel",
128128
]

src/mcp/server/fastmcp/resources/types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ class FunctionResource(Resource):
5454
async def read(self) -> str | bytes:
5555
"""Read the resource by calling the wrapped function."""
5656
try:
57-
result = await self.fn() if inspect.iscoroutinefunction(self.fn) else self.fn()
57+
# Call the function first to see if it returns a coroutine
58+
result = self.fn()
59+
# If it's a coroutine, await it
60+
if inspect.iscoroutine(result):
61+
result = await result
62+
5863
if isinstance(result, Resource):
5964
return await result.read()
6065
elif isinstance(result, bytes):

src/mcp/server/lowlevel/server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ def get_capabilities(
190190
resources_capability = None
191191
tools_capability = None
192192
logging_capability = None
193+
completions_capability = None
193194

194195
# Set prompt capabilities if handler exists
195196
if types.ListPromptsRequest in self.request_handlers:
@@ -209,12 +210,17 @@ def get_capabilities(
209210
if types.SetLevelRequest in self.request_handlers:
210211
logging_capability = types.LoggingCapability()
211212

213+
# Set completions capabilities if handler exists
214+
if types.CompleteRequest in self.request_handlers:
215+
completions_capability = types.CompletionsCapability()
216+
212217
return types.ServerCapabilities(
213218
prompts=prompts_capability,
214219
resources=resources_capability,
215220
tools=tools_capability,
216221
logging=logging_capability,
217222
experimental=experimental_capabilities,
223+
completions=completions_capability,
218224
)
219225

220226
@property

src/mcp/types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ class LoggingCapability(BaseModel):
286286
model_config = ConfigDict(extra="allow")
287287

288288

289+
class CompletionsCapability(BaseModel):
290+
"""Capability for completions operations."""
291+
292+
model_config = ConfigDict(extra="allow")
293+
294+
289295
class ServerCapabilities(BaseModel):
290296
"""Capabilities that a server may support."""
291297

@@ -299,6 +305,8 @@ class ServerCapabilities(BaseModel):
299305
"""Present if the server offers any resources to read."""
300306
tools: ToolsCapability | None = None
301307
"""Present if the server offers any tools to call."""
308+
completions: CompletionsCapability | None = None
309+
"""Present if the server offers autocompletion suggestions for prompts and resources."""
302310
model_config = ConfigDict(extra="allow")
303311

304312

tests/issues/test_188_concurrency.py

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,84 @@
33
from pydantic import AnyUrl
44

55
from mcp.server.fastmcp import FastMCP
6-
from mcp.shared.memory import (
7-
create_connected_server_and_client_session as create_session,
8-
)
9-
10-
_sleep_time_seconds = 0.01
11-
_resource_name = "slow://slow_resource"
6+
from mcp.shared.memory import create_connected_server_and_client_session as create_session
127

138

149
@pytest.mark.anyio
15-
async def test_messages_are_executed_concurrently():
10+
async def test_messages_are_executed_concurrently_tools():
1611
server = FastMCP("test")
17-
call_timestamps = []
12+
event = anyio.Event()
13+
tool_started = anyio.Event()
14+
call_order = []
1815

1916
@server.tool("sleep")
2017
async def sleep_tool():
21-
call_timestamps.append(("tool_start_time", anyio.current_time()))
22-
await anyio.sleep(_sleep_time_seconds)
23-
call_timestamps.append(("tool_end_time", anyio.current_time()))
18+
call_order.append("waiting_for_event")
19+
tool_started.set()
20+
await event.wait()
21+
call_order.append("tool_end")
2422
return "done"
2523

26-
@server.resource(_resource_name)
27-
async def slow_resource():
28-
call_timestamps.append(("resource_start_time", anyio.current_time()))
29-
await anyio.sleep(_sleep_time_seconds)
30-
call_timestamps.append(("resource_end_time", anyio.current_time()))
24+
@server.tool("trigger")
25+
async def trigger():
26+
# Wait for tool to start before setting the event
27+
await tool_started.wait()
28+
call_order.append("trigger_started")
29+
event.set()
30+
call_order.append("trigger_end")
3131
return "slow"
3232

3333
async with create_session(server._mcp_server) as client_session:
34+
# First tool will wait on event, second will set it
3435
async with anyio.create_task_group() as tg:
35-
for _ in range(10):
36-
tg.start_soon(client_session.call_tool, "sleep")
37-
tg.start_soon(client_session.read_resource, AnyUrl(_resource_name))
36+
# Start the tool first (it will wait on event)
37+
tg.start_soon(client_session.call_tool, "sleep")
38+
# Then the trigger tool will set the event to allow the first tool to continue
39+
await client_session.call_tool("trigger")
3840

39-
active_calls = 0
40-
max_concurrent_calls = 0
41-
for call_type, _ in sorted(call_timestamps, key=lambda x: x[1]):
42-
if "start" in call_type:
43-
active_calls += 1
44-
max_concurrent_calls = max(max_concurrent_calls, active_calls)
45-
else:
46-
active_calls -= 1
47-
print(f"Max concurrent calls: {max_concurrent_calls}")
48-
assert max_concurrent_calls > 1, "No concurrent calls were executed"
41+
# Verify that both ran concurrently
42+
assert call_order == [
43+
"waiting_for_event",
44+
"trigger_started",
45+
"trigger_end",
46+
"tool_end",
47+
], f"Expected concurrent execution, but got: {call_order}"
4948

5049

51-
def main():
52-
anyio.run(test_messages_are_executed_concurrently)
50+
@pytest.mark.anyio
51+
async def test_messages_are_executed_concurrently_tools_and_resources():
52+
server = FastMCP("test")
53+
event = anyio.Event()
54+
tool_started = anyio.Event()
55+
call_order = []
5356

57+
@server.tool("sleep")
58+
async def sleep_tool():
59+
call_order.append("waiting_for_event")
60+
tool_started.set()
61+
await event.wait()
62+
call_order.append("tool_end")
63+
return "done"
5464

55-
if __name__ == "__main__":
56-
import logging
65+
@server.resource("slow://slow_resource")
66+
async def slow_resource():
67+
# Wait for tool to start before setting the event
68+
await tool_started.wait()
69+
event.set()
70+
call_order.append("resource_end")
71+
return "slow"
5772

58-
logging.basicConfig(level=logging.DEBUG)
73+
async with create_session(server._mcp_server) as client_session:
74+
# First tool will wait on event, second will set it
75+
async with anyio.create_task_group() as tg:
76+
# Start the tool first (it will wait on event)
77+
tg.start_soon(client_session.call_tool, "sleep")
78+
# Then the resource (it will set the event)
79+
tg.start_soon(client_session.read_resource, AnyUrl("slow://slow_resource"))
5980

60-
main()
81+
# Verify that both ran concurrently
82+
assert call_order == [
83+
"waiting_for_event",
84+
"resource_end",
85+
"tool_end",
86+
], f"Expected concurrent execution, but got: {call_order}"

tests/server/fastmcp/test_func_metadata.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import annotated_types
55
import pytest
6+
from dirty_equals import IsPartialDict
67
from pydantic import BaseModel, Field
78

89
from mcp.server.fastmcp.utilities.func_metadata import func_metadata
@@ -202,11 +203,8 @@ def func_dict_any() -> dict[str, Any]:
202203
return {"a": 1, "b": "hello", "c": [1, 2, 3]}
203204

204205
meta = func_metadata(func_dict_any)
205-
assert meta.output_schema == {
206-
"additionalProperties": True,
207-
"type": "object",
208-
"title": "func_dict_anyDictOutput",
209-
}
206+
207+
assert meta.output_schema == IsPartialDict(type="object", title="func_dict_anyDictOutput")
210208

211209
# Test dict[str, str]
212210
def func_dict_str() -> dict[str, str]:

tests/server/test_session.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
from mcp.shared.session import RequestResponder
1212
from mcp.types import (
1313
ClientNotification,
14+
Completion,
15+
CompletionArgument,
16+
CompletionsCapability,
1417
InitializedNotification,
18+
PromptReference,
1519
PromptsCapability,
20+
ResourceReference,
1621
ResourcesCapability,
1722
ServerCapabilities,
1823
)
@@ -80,6 +85,7 @@ async def test_server_capabilities():
8085
caps = server.get_capabilities(notification_options, experimental_capabilities)
8186
assert caps.prompts is None
8287
assert caps.resources is None
88+
assert caps.completions is None
8389

8490
# Add a prompts handler
8591
@server.list_prompts()
@@ -89,6 +95,7 @@ async def list_prompts():
8995
caps = server.get_capabilities(notification_options, experimental_capabilities)
9096
assert caps.prompts == PromptsCapability(listChanged=False)
9197
assert caps.resources is None
98+
assert caps.completions is None
9299

93100
# Add a resources handler
94101
@server.list_resources()
@@ -98,6 +105,19 @@ async def list_resources():
98105
caps = server.get_capabilities(notification_options, experimental_capabilities)
99106
assert caps.prompts == PromptsCapability(listChanged=False)
100107
assert caps.resources == ResourcesCapability(subscribe=False, listChanged=False)
108+
assert caps.completions is None
109+
110+
# Add a complete handler
111+
@server.completion()
112+
async def complete(ref: PromptReference | ResourceReference, argument: CompletionArgument):
113+
return Completion(
114+
values=["completion1", "completion2"],
115+
)
116+
117+
caps = server.get_capabilities(notification_options, experimental_capabilities)
118+
assert caps.prompts == PromptsCapability(listChanged=False)
119+
assert caps.resources == ResourcesCapability(subscribe=False, listChanged=False)
120+
assert caps.completions == CompletionsCapability()
101121

102122

103123
@pytest.mark.anyio

0 commit comments

Comments
 (0)