Building Agents with Tool Discovery
The MCP Rank SDK makes it easy to build AI agents that automatically discover and use the right tools for any task.The Agent Class
TheAgent class wraps OpenAI or Anthropic clients and handles:
- Searching for relevant MCP servers
- Discovering tools from those servers
- Formatting tools for the model
- Executing tool calls
- Managing the conversation loop
Supported Models
OpenAI
Anthropic
How Tool Discovery Works
- Search: When you call
agent.run(), we search for MCP servers matching your query - Discovery: We connect to the top servers and list their tools
- Format: Tools are formatted for the model (OpenAI function calling or Anthropic tools)
- Execute: When the model calls a tool, we invoke it on the MCP server
- Loop: The conversation continues until the model responds without tool calls
Custom Configuration
Error Handling
- Tool not found: No MCP server matched the query
- Tool execution failed: The MCP server returned an error
- Max iterations: Agent hit
max_tool_callslimit
Improving Results
Be Specific
Provide Context
Direct Tool Usage
For more control, use theMCPRankClient directly: