Skip to content

Commit a378a50

Browse files
author
Tapan Chugh
committed
refactor: consolidate URI handling and implement DRY principles
- Add TOOL_SCHEME and PROMPT_SCHEME constants in mcp/types.py - Create common URI utilities in uri_utils.py for normalization and prefix filtering - Update Tool and Prompt classes to auto-generate URIs when not provided - Replace hardcoded URI schemes throughout codebase with constants - Fix prefix filtering to handle partial matches correctly - Update example files to match expected function signatures
1 parent 5d025a9 commit a378a50

File tree

37 files changed

+659
-120
lines changed

37 files changed

+659
-120
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ server = Server("example-server", lifespan=server_lifespan)
11251125

11261126

11271127
@server.list_tools()
1128-
async def handle_list_tools() -> list[types.Tool]:
1128+
async def handle_list_tools(request: types.ListToolsRequest) -> list[types.Tool]:
11291129
"""List available tools."""
11301130
return [
11311131
types.Tool(
@@ -1207,7 +1207,7 @@ server = Server("example-server")
12071207

12081208

12091209
@server.list_prompts()
1210-
async def handle_list_prompts() -> list[types.Prompt]:
1210+
async def handle_list_prompts(request: types.ListPromptsRequest) -> list[types.Prompt]:
12111211
"""List available prompts."""
12121212
return [
12131213
types.Prompt(
@@ -1286,7 +1286,7 @@ server = Server("example-server")
12861286

12871287

12881288
@server.list_tools()
1289-
async def list_tools() -> list[types.Tool]:
1289+
async def list_tools(request: types.ListToolsRequest) -> list[types.Tool]:
12901290
"""List available tools with structured output schemas."""
12911291
return [
12921292
types.Tool(

examples/servers/simple-prompt/mcp_simple_prompt/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def main(port: int, transport: str) -> int:
4949
app = Server("mcp-simple-prompt")
5050

5151
@app.list_prompts()
52-
async def list_prompts() -> list[types.Prompt]:
52+
async def list_prompts(request: types.ListPromptsRequest) -> list[types.Prompt]:
5353
return [
5454
types.Prompt(
5555
name="simple",

examples/servers/simple-streamablehttp-stateless/mcp_simple_streamablehttp_stateless/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def call_tool(name: str, arguments: dict) -> list[types.ContentBlock]:
6969
]
7070

7171
@app.list_tools()
72-
async def list_tools() -> list[types.Tool]:
72+
async def list_tools(request: types.ListToolsRequest) -> list[types.Tool]:
7373
return [
7474
types.Tool(
7575
name="start-notification-stream",

examples/servers/simple-streamablehttp/mcp_simple_streamablehttp/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ async def call_tool(name: str, arguments: dict) -> list[types.ContentBlock]:
8787
]
8888

8989
@app.list_tools()
90-
async def list_tools() -> list[types.Tool]:
90+
async def list_tools(request: types.ListToolsRequest) -> list[types.Tool]:
9191
return [
9292
types.Tool(
9393
name="start-notification-stream",

examples/servers/simple-tool/mcp_simple_tool/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async def fetch_tool(name: str, arguments: dict) -> list[types.ContentBlock]:
3737
return await fetch_website(arguments["url"])
3838

3939
@app.list_tools()
40-
async def list_tools() -> list[types.Tool]:
40+
async def list_tools(request: types.ListToolsRequest) -> list[types.Tool]:
4141
return [
4242
types.Tool(
4343
name="fetch",

examples/servers/structured_output_lowlevel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
@server.list_tools()
24-
async def list_tools() -> list[types.Tool]:
24+
async def list_tools(request: types.ListToolsRequest) -> list[types.Tool]:
2525
"""List available tools with their schemas."""
2626
return [
2727
types.Tool(

examples/snippets/servers/lowlevel/basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
@server.list_prompts()
18-
async def handle_list_prompts() -> list[types.Prompt]:
18+
async def handle_list_prompts(request: types.ListPromptsRequest) -> list[types.Prompt]:
1919
"""List available prompts."""
2020
return [
2121
types.Prompt(

examples/snippets/servers/lowlevel/lifespan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def server_lifespan(_server: Server) -> AsyncIterator[dict]:
4949

5050

5151
@server.list_tools()
52-
async def handle_list_tools() -> list[types.Tool]:
52+
async def handle_list_tools(request: types.ListToolsRequest) -> list[types.Tool]:
5353
"""List available tools."""
5454
return [
5555
types.Tool(

examples/snippets/servers/lowlevel/structured_output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
@server.list_tools()
18-
async def list_tools() -> list[types.Tool]:
18+
async def list_tools(request: types.ListToolsRequest) -> list[types.Tool]:
1919
"""List available tools with structured output schemas."""
2020
return [
2121
types.Tool(

src/mcp/client/session.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,16 @@ async def _validate_tool_result(self, name: str, result: types.CallToolResult) -
342342
except SchemaError as e:
343343
raise RuntimeError(f"Invalid schema for tool {name}: {e}")
344344

345-
async def list_prompts(self, cursor: str | None = None) -> types.ListPromptsResult:
345+
async def list_prompts(self, prefix: str | None = None, cursor: str | None = None) -> types.ListPromptsResult:
346346
"""Send a prompts/list request."""
347+
params = None
348+
if cursor is not None or prefix is not None:
349+
params = types.ListRequestParams(prefix=prefix, cursor=cursor)
347350
return await self.send_request(
348351
types.ClientRequest(
349352
types.ListPromptsRequest(
350353
method="prompts/list",
351-
params=types.ListRequestParams(cursor=cursor) if cursor is not None else None,
354+
params=params,
352355
)
353356
),
354357
types.ListPromptsResult,
@@ -391,13 +394,16 @@ async def complete(
391394
types.CompleteResult,
392395
)
393396

394-
async def list_tools(self, cursor: str | None = None) -> types.ListToolsResult:
397+
async def list_tools(self, prefix: str | None = None, cursor: str | None = None) -> types.ListToolsResult:
395398
"""Send a tools/list request."""
399+
params = None
400+
if cursor is not None or prefix is not None:
401+
params = types.ListRequestParams(prefix=prefix, cursor=cursor)
396402
result = await self.send_request(
397403
types.ClientRequest(
398404
types.ListToolsRequest(
399405
method="tools/list",
400-
params=types.ListRequestParams(cursor=cursor) if cursor is not None else None,
406+
params=params,
401407
)
402408
),
403409
types.ListToolsResult,

0 commit comments

Comments
 (0)