Skip to main content

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

The Agent 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

  1. Search: When you call agent.run(), we search for MCP servers matching your query
  2. Discovery: We connect to the top servers and list their tools
  3. Format: Tools are formatted for the model (OpenAI function calling or Anthropic tools)
  4. Execute: When the model calls a tool, we invoke it on the MCP server
  5. Loop: The conversation continues until the model responds without tool calls

Custom Configuration

Error Handling

Common errors:
  • Tool not found: No MCP server matched the query
  • Tool execution failed: The MCP server returned an error
  • Max iterations: Agent hit max_tool_calls limit

Improving Results

Be Specific

Provide Context

Direct Tool Usage

For more control, use the MCPRankClient directly: