Skip to content

fix: MCP filtering, make agent and run ctx optional #977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/agents/mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ async def cleanup(self):
@abc.abstractmethod
async def list_tools(
self,
run_context: RunContextWrapper[Any],
agent: Agent[Any],
run_context: RunContextWrapper[Any] | None = None,
agent: Agent[Any] | None = None,
) -> list[MCPTool]:
"""List the tools available on the server."""
pass
Expand Down Expand Up @@ -231,8 +231,8 @@ async def connect(self):

async def list_tools(
self,
run_context: RunContextWrapper[Any],
agent: Agent[Any],
run_context: RunContextWrapper[Any] | None = None,
agent: Agent[Any] | None = None,
) -> list[MCPTool]:
"""List the tools available on the server."""
if not self.session:
Expand All @@ -251,6 +251,8 @@ async def list_tools(
# Filter tools based on tool_filter
filtered_tools = tools
if self.tool_filter is not None:
if run_context is None or agent is None:
raise UserError("run_context and agent are required for dynamic tool filtering")
filtered_tools = await self._apply_tool_filter(filtered_tools, run_context, agent)
return filtered_tools

Expand Down