Skip to content

Commit f03b4c8

Browse files
committed
docs: tweak a few examples for stable runs
1 parent 64e411f commit f03b4c8

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

examples/mcp/get_all_mcp_tools_example/main.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ async def resolve_interruptions(agent: Agent, result: Any) -> Any:
6060
async def main():
6161
current_dir = os.path.dirname(os.path.abspath(__file__))
6262
samples_dir = os.path.join(current_dir, "sample_files")
63+
blocked_path = os.path.join(samples_dir, "test.txt")
6364

6465
async with MCPServerStdio(
6566
name="Filesystem Server",
6667
params={
6768
"command": "npx",
6869
"args": ["-y", "@modelcontextprotocol/server-filesystem", samples_dir],
70+
"cwd": samples_dir,
6971
},
7072
require_approval={"always": {"tool_names": ["read_text_file"]}},
7173
) as server:
@@ -83,10 +85,16 @@ async def main():
8385
# Build an agent that uses the prefetched tools instead of mcp_servers.
8486
prefetched_agent = Agent(
8587
name="Prefetched MCP Assistant",
86-
instructions="Use the prefetched tools to help with file questions.",
88+
instructions=(
89+
"Use the prefetched tools to help with file questions. "
90+
"When using path arguments, prefer absolute paths in the allowed directory."
91+
),
8792
tools=all_tools,
8893
)
89-
message = "List the available files and read one of them."
94+
message = (
95+
f"List files in this allowed directory: {samples_dir}. "
96+
"Then read one of those files."
97+
)
9098
print(f"\nRunning: {message}\n")
9199
result = await Runner.run(prefetched_agent, message)
92100
result = await resolve_interruptions(prefetched_agent, result)
@@ -105,10 +113,17 @@ async def main():
105113

106114
filtered_agent = Agent(
107115
name="Filtered MCP Assistant",
108-
instructions="Use the filtered tools to respond.",
116+
instructions=(
117+
"Use the filtered tools to respond. "
118+
"If a request requires a missing tool, explain that the capability is not "
119+
"available."
120+
),
109121
tools=filtered_tools,
110122
)
111-
blocked_message = "Create a file named sample_files/test.txt with the text hello."
123+
blocked_message = (
124+
f'Create a file named "{blocked_path}" with the text "hello". '
125+
"If the available tools cannot create files, explain that clearly."
126+
)
112127
print(f"\nRunning: {blocked_message}\n")
113128
filtered_result = await Runner.run(filtered_agent, blocked_message)
114129
filtered_result = await resolve_interruptions(filtered_agent, filtered_result)

examples/mcp/sse_example/server.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import random
22

3-
import requests
43
from mcp.server.fastmcp import FastMCP
54

65
# Create server
@@ -23,16 +22,16 @@ def get_secret_word() -> str:
2322
@mcp.tool()
2423
def get_current_weather(city: str) -> str:
2524
print(f"[debug-server] get_current_weather({city})")
26-
# Avoid slow or flaky network calls during automated runs.
27-
try:
28-
endpoint = "https://wttr.in"
29-
response = requests.get(f"{endpoint}/{city}", timeout=2)
30-
if response.ok:
31-
return response.text
32-
except Exception:
33-
pass
34-
# Fallback keeps the tool responsive even when offline.
35-
return f"Weather data unavailable right now; assume clear skies in {city}."
25+
# Keep tool output deterministic so this example is stable in CI and offline environments.
26+
weather_by_city = {
27+
"tokyo": "sunny with a light breeze and 20°C",
28+
"san francisco": "cool and foggy with 14°C",
29+
"new york": "partly cloudy with 18°C",
30+
}
31+
forecast = weather_by_city.get(city.strip().lower())
32+
if forecast:
33+
return f"The weather in {city} is {forecast}."
34+
return f"The weather data for {city} is unavailable in this demo."
3635

3736

3837
if __name__ == "__main__":

examples/mcp/tool_filter_example/main.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ async def run_with_auto_approval(agent: Agent[Any], message: str) -> str | None:
2424
async def main():
2525
current_dir = os.path.dirname(os.path.abspath(__file__))
2626
samples_dir = os.path.join(current_dir, "sample_files")
27+
target_path = os.path.join(samples_dir, "test.txt")
2728

2829
async with MCPServerStdio(
2930
name="Filesystem Server with filter",
3031
params={
3132
"command": "npx",
3233
"args": ["-y", "@modelcontextprotocol/server-filesystem", samples_dir],
34+
"cwd": samples_dir,
3335
},
3436
require_approval="always",
3537
tool_filter=create_static_tool_filter(
@@ -39,19 +41,28 @@ async def main():
3941
) as server:
4042
agent = Agent(
4143
name="MCP Assistant",
42-
instructions="Use the filesystem tools to answer questions.",
44+
instructions=(
45+
"Use only the available filesystem tools. "
46+
"All file paths should be absolute paths inside the allowed directory. "
47+
"If a user asks for an action that requires an unavailable tool, "
48+
"explicitly explain that it is blocked by the tool filter."
49+
),
4350
mcp_servers=[server],
4451
)
4552
trace_id = gen_trace_id()
4653
with trace(workflow_name="MCP Tool Filter Example", trace_id=trace_id):
4754
print(f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}\n")
4855
result = await run_with_auto_approval(
49-
agent, "List the files in the sample_files directory."
56+
agent, f"List the files in this allowed directory: {samples_dir}"
5057
)
5158
print(result)
5259

5360
blocked_result = await run_with_auto_approval(
54-
agent, 'Create a file named sample_files/test.txt with the text "hello".'
61+
agent,
62+
(
63+
f'Create a file at "{target_path}" with the text "hello". '
64+
"If you cannot, explain that write operations are blocked by the tool filter."
65+
),
5566
)
5667
print("\nAttempting to write a file (should be blocked):")
5768
print(blocked_result)

0 commit comments

Comments
 (0)