Skip to content

Tools and MCP ​

Tools let an agent read product data and request side effects through typed contracts your application owns. Define local tools with createTool(), connect remote tools over MCP with @anvia/mcp, and search large catalogs so the model sees only the relevant definitions.

When to use it ​

  • The agent needs live data or actions from your services, such as orders, tickets, or deployments.
  • A side effect such as a refund or message should pause for human approval before it runs.
  • An external MCP server already exposes the capability you need.
  • The tool catalog is large enough that sending every definition adds noise; use a tool index or searchTool().
  • The agent should follow reusable procedures packaged as skills.

How it works ​

The model sees each tool's name, description, and JSON schema. When it requests a tool, Anvia validates the arguments against inputSchema, calls your handler, validates the result against outputSchema if present, and sends the result back into the run. requiresApproval suspends the run with an interaction outcome before the handler executes.

ts
import { Agent, createTool } from '@anvia/core'
import { OpenAIClient } from '@anvia/openai'
import { z } from 'zod'

const client = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! })
const model = client.completionModel({ modelId: 'gpt-5.6-sol', api: 'responses' })

const refundOrder = createTool({
  name: 'refund_order',
  description: 'Refund an eligible order.',
  inputSchema: z.object({ orderId: z.string() }),
  requiresApproval: ({ orderId }) => ({ reason: `Approve refund for ${orderId}` }),
  handler: async ({ orderId }) => billing.refund(orderId),
})

const agent = new Agent({ id: 'billing', model, tools: [refundOrder], maxTurns: 3 })

billing is your application's billing service. Use handler; execute is deprecated and will be removed.

Beyond local tools:

  • Questions. createQuestionTool() from @anvia/core/tool lets the model ask structured clarifying questions, returned as a tool-question interaction.
  • Dynamic tools. createToolIndex() from @anvia/core/tool embeds a catalog and exposes only matching definitions each turn. searchTool() gives the model one tool that searches your entries, such as runbooks. Pass either in Agent.tools.
  • MCP. new McpClient({ name, transport }) from @anvia/mcp connects over stdio or streamable HTTP; await client.connect() returns a server you register through mcpServers, not tools. Close the client when you are done.
  • Skills. loadSkills(skill.local('skills')) from @anvia/core/skills loads SKILL.md packages; pass the result as skills so the agent loads detailed guidance only when a task matches.

What your application owns ​

  • Authorization, tenant scope, rate limits, and audit logging inside every handler. Schemas validate shape, not permission.
  • Approval decisions from an authenticated approver, and idempotent side effects.
  • MCP credentials, connection lifetime, and the reviewed subset of remote tools you register. An MCP connection does not grant product authorization.
  • Filtering tool results and remote output before they reach a browser.
  • Trust in skill directories and the scripts they may run.

Go deeper ​

Built for Anvia.