Skip to content

Commit a28f47a

Browse files
committed
chore: Move common path discovery to a utils script
1 parent cb901dc commit a28f47a

File tree

7 files changed

+56
-10
lines changed

7 files changed

+56
-10
lines changed

agents_mcp_usage/basic_mcp/basic_mcp_use/langgraph_mcp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from mcp import ClientSession, StdioServerParameters
1010
from mcp.client.stdio import stdio_client
1111

12+
from agents_mcp_usage.utils import get_mcp_server_path
13+
1214
load_dotenv()
1315

1416
# Configure logging if LOGFIRE_TOKEN is set
@@ -21,7 +23,7 @@
2123
# Create server parameters for stdio connection
2224
server = StdioServerParameters(
2325
command="uv",
24-
args=["run", "mcp_servers/example_server.py", "stdio"],
26+
args=["run", str(get_mcp_server_path("example_server.py")), "stdio"],
2527
)
2628

2729
model = ChatGoogleGenerativeAI(

agents_mcp_usage/basic_mcp/basic_mcp_use/oai-agent_mcp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from agents.mcp import MCPServerStdio
66
from dotenv import load_dotenv
77

8+
from agents_mcp_usage.utils import get_mcp_server_path
9+
810
load_dotenv()
911

1012
# Configure Logfire
@@ -26,7 +28,7 @@ async def main(query: str = "Greet Andrew and give him the current time") -> Non
2628
async with MCPServerStdio(
2729
params={
2830
"command": "uv",
29-
"args": ["run", "mcp_servers/example_server.py", "stdio"],
31+
"args": ["run", str(get_mcp_server_path("example_server.py")), "stdio"],
3032
}
3133
) as server:
3234
# Initialise the agent with the server

agents_mcp_usage/basic_mcp/basic_mcp_use/pydantic_mcp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from pydantic_ai import Agent
66
from pydantic_ai.mcp import MCPServerStdio
77

8+
from agents_mcp_usage.utils import get_mcp_server_path
9+
810
load_dotenv()
911

1012
# Configure logging to logfire if LOGFIRE_TOKEN is set in environment
@@ -16,7 +18,7 @@
1618
command="uv",
1719
args=[
1820
"run",
19-
"mcp_servers/example_server.py",
21+
str(get_mcp_server_path("example_server.py")),
2022
"stdio",
2123
],
2224
)

agents_mcp_usage/multi_mcp/eval_multi_mcp/evals_pydantic_mcp.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
invalid_mermaid_diagram_hard,
3838
valid_mermaid_diagram,
3939
)
40-
from mcp_servers.mermaid_validator import validate_mermaid_diagram
40+
from agents_mcp_usage.utils import get_mcp_server_path
41+
import sys
42+
import importlib.util
4143

4244
load_dotenv()
4345

@@ -57,6 +59,14 @@
5759
BASE_RETRY_DELAY = 1.0 # seconds
5860
MAX_RETRY_DELAY = 30.0 # seconds
5961

62+
# Dynamically import validate_mermaid_diagram from the mermaid_validator.py file
63+
mermaid_validator_path = str(get_mcp_server_path("mermaid_validator.py"))
64+
spec = importlib.util.spec_from_file_location("mermaid_validator", mermaid_validator_path)
65+
mermaid_validator = importlib.util.module_from_spec(spec)
66+
sys.modules["mermaid_validator"] = mermaid_validator
67+
spec.loader.exec_module(mermaid_validator)
68+
validate_mermaid_diagram = mermaid_validator.validate_mermaid_diagram
69+
6070
# ============================================================================
6171
# Retry Utilities
6272
# ============================================================================
@@ -177,15 +187,15 @@ def get_mcp_servers() -> List[MCPServerStdio]:
177187
command="uv",
178188
args=[
179189
"run",
180-
"mcp_servers/example_server.py",
190+
str(get_mcp_server_path("example_server.py")),
181191
"stdio",
182192
],
183193
)
184194
mermaid_server = MCPServerStdio(
185195
command="uv",
186196
args=[
187197
"run",
188-
"mcp_servers/mermaid_validator.py",
198+
str(get_mcp_server_path("mermaid_validator.py")),
189199
],
190200
)
191201
return [local_server, mermaid_server]

agents_mcp_usage/multi_mcp/multi_mcp_use/adk_mcp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from google.genai import types
3232

3333
from agents_mcp_usage.multi_mcp.mermaid_diagrams import invalid_mermaid_diagram_easy
34+
from agents_mcp_usage.utils import get_mcp_server_path
3435

3536
load_dotenv()
3637

@@ -62,7 +63,7 @@ async def get_tools_async() -> tuple[list, list]:
6263
command="uv",
6364
args=[
6465
"run",
65-
"mcp_servers/example_server.py",
66+
str(get_mcp_server_path("example_server.py")),
6667
"stdio",
6768
],
6869
)
@@ -71,7 +72,7 @@ async def get_tools_async() -> tuple[list, list]:
7172
command="uv",
7273
args=[
7374
"run",
74-
"mcp_servers/mermaid_validator.py",
75+
str(get_mcp_server_path("mermaid_validator.py")),
7576
],
7677
)
7778

agents_mcp_usage/multi_mcp/multi_mcp_use/pydantic_mcp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pydantic_ai.usage import UsageLimits
99

1010
from agents_mcp_usage.multi_mcp.mermaid_diagrams import invalid_mermaid_diagram_easy
11+
from agents_mcp_usage.utils import get_mcp_server_path
1112

1213
load_dotenv()
1314

@@ -21,15 +22,15 @@
2122
command="uv",
2223
args=[
2324
"run",
24-
"mcp_servers/example_server.py",
25+
str(get_mcp_server_path("example_server.py")),
2526
"stdio",
2627
],
2728
)
2829
mermaid_server = MCPServerStdio(
2930
command="uv",
3031
args=[
3132
"run",
32-
"mcp_servers/mermaid_validator.py",
33+
str(get_mcp_server_path("mermaid_validator.py")),
3334
],
3435
)
3536
# Create Agent with MCP servers

agents_mcp_usage/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Utility functions and constants for the agents-mcp-usage project."""
2+
3+
from pathlib import Path
4+
5+
# Get the project root directory (where pyproject.toml is located)
6+
# This is 2 levels up from this utils.py file: agents_mcp_usage/utils.py -> agents-mcp-usage/
7+
PROJECT_ROOT = Path(__file__).parent.parent.resolve()
8+
9+
10+
def get_project_root() -> Path:
11+
"""Get the absolute path to the project root directory.
12+
13+
Returns:
14+
Path: The absolute path to the project root where pyproject.toml is located.
15+
"""
16+
return PROJECT_ROOT
17+
18+
19+
def get_mcp_server_path(server_name: str) -> Path:
20+
"""Get the absolute path to an MCP server file.
21+
22+
Args:
23+
server_name: The name of the MCP server file (e.g., "example_server.py")
24+
25+
Returns:
26+
Path: The absolute path to the MCP server file.
27+
"""
28+
return PROJECT_ROOT / "mcp_servers" / server_name

0 commit comments

Comments
 (0)