Skip to content

Agent tools and mcp_servers should allow for Callable and accept RunContext#2167

@hayescode

Description

@hayescode

Many MCP servers (and other tools) requires authentication. It is not straight-forward to perform authentication and refresh authentication tokens for servers, especially when defining a single agent to be used in a multi-user platform.

Pydantic-AI solves this by allowing tools/mcp servers to be both definitions and callable that accept RunContext and return a definition. Adding something like this would greatly improve scalability, ease of use, and adoption of OpenAI Agents SDK.

Current

fromagentsimportAgent, HostedMCPTool, OpenAIResponsesModel, Runnerfromopenai.types.responses.tool_paramimportMcpfrompydanticimportBaseModelclassAgentContext(BaseModel): user_id: struser_github_token: stragent_context=AgentContext( user_id="user_12345", user_github_token="ghp_XXXXXXXXXXXXXXXXXXXXXX", ) github_mcp_config=HostedMCPTool( tool_config=Mcp( server_label="github", server_url="https://api.githubcopilot.com/mcp/", type="mcp", # authorization="TOKEN_PLACEHOLDER",require_approval="never" ), ) github_agent=Agent( name="GitHub MCP Agent", tools=[github_mcp_config], model=OpenAIResponsesModel(model="gpt-5.1",openai_client=openai_client), ) github_mcp_config.tool_config.authorization=agent_context.user_github_tokenresult=awaitRunner.run(github_agent, "What is my github username?",context=agent_context) print(result.final_output)

Proposed

fromagentsimportAgent, HostedMCPTool, OpenAIResponsesModel, Runner, RunContextfromopenai.types.responses.tool_paramimportMcpfrompydanticimportBaseModelclassAgentContext(BaseModel): user_id: struser_github_token: stragent_context=AgentContext( user_id="user_12345", user_github_token="ghp_XXXXXXXXXXXXXXXXXXXXXX", ) asyncdefget_github_mcp(ctx: RunContext[AgentContext]) ->HostedMCPTool: user_token=ctx.context.user_github_tokenreturnHostedMCPTool( tool_config=Mcp( server_label="github", server_url="https://api.githubcopilot.com/mcp/", type="mcp", authorization=user_token, require_approval="never" ), ) github_agent=Agent( name="GitHub MCP Agent", tools=[get_github_mcp], model=OpenAIResponsesModel(model="gpt-5.1",openai_client=openai_client), ) result=awaitRunner.run(github_agent, "What is my github username?",context=agent_context) print(result.final_output)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions