Skip to content

Commit a45b8e1

Browse files
authored
Expose the StdioServerParameters.cwd param (#1514)
1 parent 1a33c02 commit a45b8e1

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

pydantic_ai_slim/pydantic_ai/mcp.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from collections.abc import AsyncIterator, Sequence
55
from contextlib import AsyncExitStack, asynccontextmanager
66
from dataclasses import dataclass
7+
from pathlib import Path
78
from types import TracebackType
89
from typing import Any
910

@@ -150,13 +151,16 @@ async def main():
150151
If you want to inherit the environment variables from the parent process, use `env=os.environ`.
151152
"""
152153

154+
cwd: str | Path | None = None
155+
"""The working directory to use when spawning the process."""
156+
153157
@asynccontextmanager
154158
async def client_streams(
155159
self,
156160
) -> AsyncIterator[
157161
tuple[MemoryObjectReceiveStream[JSONRPCMessage | Exception], MemoryObjectSendStream[JSONRPCMessage]]
158162
]:
159-
server = StdioServerParameters(command=self.command, args=list(self.args), env=self.env)
163+
server = StdioServerParameters(command=self.command, args=list(self.args), env=self.env, cwd=self.cwd)
160164
async with stdio_client(server=server) as (read_stream, write_stream):
161165
yield read_stream, write_stream
162166

pydantic_ai_slim/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ tavily = ["tavily-python>=0.5.0"]
6969
# CLI
7070
cli = ["rich>=13", "prompt-toolkit>=3", "argcomplete>=3.5.0"]
7171
# MCP
72-
mcp = ["mcp>=1.4.1; python_version >= '3.10'"]
72+
mcp = ["mcp>=1.5.0; python_version >= '3.10'"]
7373
# Evals
7474
evals = ["pydantic-evals=={{ version }}"]
7575

tests/test_mcp.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tests for the MCP (Model Context Protocol) server implementation."""
22

3+
from pathlib import Path
4+
35
import pytest
46
from dirty_equals import IsInstance
57
from inline_snapshot import snapshot
@@ -38,6 +40,14 @@ async def test_stdio_server():
3840
assert result.content == snapshot([TextContent(type='text', text='32.0')])
3941

4042

43+
async def test_stdio_server_with_cwd():
44+
test_dir = Path(__file__).parent
45+
server = MCPServerStdio('python', ['mcp_server.py'], cwd=test_dir)
46+
async with server:
47+
tools = await server.list_tools()
48+
assert len(tools) == 1
49+
50+
4151
def test_sse_server():
4252
sse_server = MCPServerHTTP(url='http://localhost:8000/sse')
4353
assert sse_server.url == 'http://localhost:8000/sse'

uv.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)