I’m trying to use local instances of Playwright and Selenium MCP Servers under Agents SDK through STDIO using MCPServerStdio .
It runs correctly, but it appears to leave the web browser up after completion.
when I call await mcp_server.cleanup() it hangs.
What’s the proper way to clean up (dispose) local MCP servers running over STDIO?
import asyncio
import shutil
from dotenv import load_dotenv
load_dotenv()
from agents import Agent, Runner, gen_trace_id, trace
from agents.mcp import MCPServer, MCPServerStdio
# https://github.com/openai/openai-agents-python/blob/main/examples/mcp/filesystem_example/main.py
async def run(mcp_server: MCPServer):
agent = Agent(
name="The Web Browsing Assistant",
instructions="Use the MCP tools to read the world wide web and answer questions based on those files. After you are done, make sure to shut down the MCP tools.",
mcp_servers=[mcp_server],
model="gpt-4.1"
)
message = "Load web page www.wikipedia.org and find out the date when George Washington died. Output just the date in yyyy-MM-dd format. Do not output any other text, just the date."
print(f"Running: {message}")
result = await Runner.run(starting_agent=agent, input=message)
print(result.final_output)
print("Cleanup")
await mcp_server.cleanup()
print("Exit.")
async def main():
async with MCPServerStdio(
name="Playwright MCP Server, via npx",
params={
"command": "npx",
"args": ["-y", "@executeautomation/playwright-mcp-server"],
},
client_session_timeout_seconds = 60
) as server:
trace_id = gen_trace_id()
with trace(workflow_name="MCP Playwright Example", trace_id=trace_id):
print(f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}\n")
await run(server)
# async with MCPServerStdio(
# name="Selenium MCP Server, via npx",
# params={
# "command": "npx",
# "args": ["-y", "@angiejones/mcp-selenium"],
# },
# client_session_timeout_seconds = 60
# ) as server:
# trace_id = gen_trace_id()
# with trace(workflow_name="MCP Selenium Example", trace_id=trace_id):
# print(f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}\n")
# await run(server)
if __name__ == "__main__":
# Let's make sure the user has npx installed
if not shutil.which("npx"):
raise RuntimeError("npx is not installed. Please install it with `npm install -g npx`.")
asyncio.run(main())
How can I get the browser to