-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
Milestone
Description
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
from agents import Agent, HostedMCPTool, OpenAIResponsesModel, Runner
from openai.types.responses.tool_param import Mcp
from pydantic import BaseModel
class AgentContext(BaseModel):
user_id: str
user_github_token: str
agent_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_token
result = await Runner.run(github_agent, "What is my github username?",context=agent_context)
print(result.final_output)Proposed
from agents import Agent, HostedMCPTool, OpenAIResponsesModel, Runner, RunContext
from openai.types.responses.tool_param import Mcp
from pydantic import BaseModel
class AgentContext(BaseModel):
user_id: str
user_github_token: str
agent_context = AgentContext(
user_id="user_12345",
user_github_token="ghp_XXXXXXXXXXXXXXXXXXXXXX",
)
async def get_github_mcp(ctx: RunContext[AgentContext]) -> HostedMCPTool:
user_token = ctx.context.user_github_token
return HostedMCPTool(
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 = await Runner.run(github_agent, "What is my github username?",context=agent_context)
print(result.final_output)