Tools & MCP

Tools let agents interact with the outside world. Define tools with the @tool decorator, connect to MCP servers for external tool discovery, or use the connector framework for managed integrations.

from sagewai import tool, UniversalAgent

@tool
async def get_weather(city: str) -> str:
    """Get current weather for a city."""
    return f"Sunny in {city}"

agent = UniversalAgent(name="bot", model="gpt-4o", tools=[get_weather])

@tool Decorator

Convert a typed Python function into a ToolSpec with auto-generated JSON Schema. The decorator extracts the name, description, and parameters from the function signature and docstring.

from sagewai import tool

@tool
async def search_docs(query: str, limit: int = 5) -> str:
    """Search the knowledge base.

    Args:
        query: Search query string.
        limit: Maximum results to return.
    """
    return f"Found {limit} results for: {query}"

Behavior

  • Name: taken from fn.__name__
  • Description: taken from the docstring
  • Parameters: auto-generated JSON Schema from type hints
  • Required params: parameters without defaults are marked required
  • Return type: the function must return a string

ToolSpec

Manual tool definition. Use when you need full control over the JSON Schema.

from sagewai import ToolSpec

spec = ToolSpec(
    name="search",
    description="Search the knowledge base",
    parameters={
        "type": "object",
        "properties": {
            "query": {"type": "string", "description": "Search query"},
        },
        "required": ["query"],
    },
    handler=search_function,
)

Fields

FieldTypeDefaultDescription
namestrrequiredTool name (alphanumeric + underscore)
descriptionstrrequiredHuman-readable description shown to the LLM
parametersdict[str, Any]{}JSON Schema for tool parameters
handlerCallable | NoneNoneAsync or sync function to execute

McpClient

Connect to MCP (Model Context Protocol) servers and discover their tools as ToolSpec instances.

connect (stdio)

Launch an MCP server as a subprocess and return its tools.

from sagewai import McpClient

tools = await McpClient.connect("npx my-mcp-server")
# or with a list:
tools = await McpClient.connect(["python", "-m", "mcp_server"])
ParameterTypeDefaultDescription
server_cmdstr | list[str]requiredCommand to launch the MCP server
envdict[str, str] | NoneNoneEnvironment variables for the subprocess

Returns: list[ToolSpec]

connect_sse (HTTP+SSE)

Connect to a remote MCP server via HTTP Server-Sent Events.

tools = await McpClient.connect_sse("http://localhost:3000/mcp")
ParameterTypeDefaultDescription
urlstrrequiredBase URL of the MCP server endpoint

Returns: list[ToolSpec]

connect_http (Streamable HTTP)

Connect via the MCP 2025-03-26 streamable HTTP transport. Session management via Mcp-Session-Id is handled automatically.

tools = await McpClient.connect_http(
    "http://localhost:3000/mcp",
    headers={"Authorization": "Bearer token"},
)
ParameterTypeDefaultDescription
urlstrrequiredMCP server endpoint URL
headersdict[str, str] | NoneNoneExtra HTTP headers (e.g. for auth)

Returns: list[ToolSpec]


ConnectorRegistry

Central registry for managed connectors. Handles credential resolution, connection lifecycle, and health monitoring.

from sagewai import ConnectorRegistry

registry = ConnectorRegistry(credential_store=my_store)

# Register a connector
registry.register(my_connector)

# Connect (credentials resolved from store or env)
conn = await registry.connect("slack")

# List all registered connectors
connectors = registry.list()

Constructor

ParameterTypeDefaultDescription
credential_storeCredentialStore | NoneNoneStore for credential resolution

Methods

MethodSignatureReturnsDescription
registerregister(connector, *, custom=False)NoneRegister a connector
unregisterunregister(name)NoneRemove a connector
getget(name)ConnectorSpecGet connector by name (raises KeyError)
listlist()list[ConnectorSpec]List all registered connectors
connectasync connect(name, credentials=None, *, resilient=True)McpConnectionConnect to a connector

ConnectorSpec

Base class for connector definitions. Uses Pydantic BaseModel.

from sagewai import ConnectorSpec
from sagewai.connectors.base import AuthType, AuthField

spec = ConnectorSpec(
    name="my-api",
    display_name="My API",
    category="productivity",
    description="Connect to My API",
    auth_type=AuthType.API_KEY,
    auth_fields=[AuthField(key="api_key", label="API Key", env_var="MY_API_KEY")],
    mcp_command=["python", "-m", "my_mcp_server"],
)

Fields

FieldTypeDefaultDescription
namestrrequiredUnique connector identifier
display_namestrrequiredHuman-readable name
categorystrrequiredCategory (e.g. "productivity", "commerce")
descriptionstrrequiredDescription of the connector
auth_typeAuthTyperequiredAuthentication type (ENV_KEY, API_KEY, OAUTH2, NONE)
auth_fieldslist[AuthField]requiredCredential fields
mcp_commandlist[str]requiredCommand to launch the MCP server
docs_urlstr | NoneNoneLink to documentation
agent_descriptionstr""Description for LLM tool selection