Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Deploy, evaluate, and manage AI agents end-to-end on Microsoft Azure AI Foundry
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
foundry-agent/invoke/invoke.md
1# Invoke Foundry Agent23Invoke deployed agents in Azure AI Foundry. Manage sessions and file operations for hosted agents.45## Quick Reference67| Property | Value |8|----------|-------|9| Agent types | Prompt (LLM-based), Hosted |10| MCP server | `azure` |11| Key Foundry MCP tools | `agent_invoke`, `agent_get`, `session_create`, `session_get`, `session_delete`, `session_list` |12| File operation tools | `session_file_upload`, `session_file_download`, `session_file_list`, `session_file_delete`, `session_file_stat`, `session_file_mkdir` |13| Conversation support | Single-turn and multi-turn (via `conversationId` for responses protocol, via session state for invocations protocol) |14| Session support | Managed sessions for hosted agents (via `session_create`) |15| Protocols | `responses` (OpenAI-compatible), `invocations` (custom payloads) |1617## When to Use This Skill1819- Send messages to a deployed agent (single or multi-turn)20- Create/manage sessions for hosted agents21- Upload/download files to/from hosted agent sessions22- Test agent behavior after creation or deployment2324## MCP Tools2526| Tool | Description | Parameters |27|------|-------------|------------|28| `agent_invoke` | Send a message to an agent and get a response | `projectEndpoint`, `agentName`, `inputText` (required); `agentVersion`, `conversationId`, `sessionId`, `protocol`, `stream` (optional) |29| `agent_get` | Get agent details to verify existence and type | `projectEndpoint` (required), `agentName` (optional) |30| `session_create` | Create a new session for a hosted agent | `projectEndpoint`, `agentName` (required); `sessionId` (optional) |31| `session_get` | Get session status and details | `projectEndpoint`, `agentName`, `sessionId` (required) |32| `session_delete` | Delete a session and release compute | `projectEndpoint`, `agentName`, `sessionId` (required) |33| `session_list` | List sessions with pagination | `projectEndpoint`, `agentName` (required); `limit`, `order`, `after`, `before` (optional) |34| `session_logstream` | Stream console logs (stdout/stderr) from a session | `projectEndpoint`, `agentName`, `sessionId` (required); `maxLines` (optional) |3536For session file operation tools (`session_file_upload`, `session_file_download`, `session_file_list`, `session_file_delete`, `session_file_stat`, `session_file_mkdir`), see [File Operations](references/file-operations.md).3738## Protocols3940Hosted agents support three protocols declared at deployment time. They are distinct contracts — pick per use case (an agent may declare more than one and serve them from the same container):4142| Protocol | Recommended Version | Route | Best For |43|----------|-------------------|-------|----------|44| `responses` | `1.0.0` | `.../agents/{agentName}/endpoint/protocols/openai/responses` | Conversational agents, OpenAI-compatible |45| `invocations` | `1.0.0` | `.../agents/{agentName}/endpoint/protocols/invocations` | Custom payloads, protocol bridges, webhook callers |46| `invocations_ws` | `1.0.0` | `wss://.../agents/endpoint/protocols/invocations_ws` | Duplex WebSocket — voice, WebRTC signaling, custom real-time streams. See the dedicated [invocations-ws skill](../invocations-ws/invocations-ws.md); `agent_invoke` does **not** speak WebSocket. |4748Key difference: `responses` takes a natural language `inputText` message with platform-managed history. `invocations` is **bytes in, bytes out** — the request body is forwarded as-is to the container and the raw response is returned. The developer defines the schema; the platform is pure pass-through. See [Invocations Protocol Guide](references/invocations-protocol.md) for I/O details, schema discovery, and examples.4950> ⚠️ **Critical for invocations:** `inputText` is forwarded as the raw HTTP request body. The agent developer defines what the container accepts. **Do not guess** — fetch the agent's OpenAPI spec or inspect its source code first.5152> 💡 **Tip:** The `agent_invoke` MCP tool supports both `responses` and `invocations` protocols. Set `protocol: 'invocations'` when targeting an invocations-protocol agent.5354## Workflow5556### Step 1: Verify Agent Readiness5758Use `agent_get` to verify the agent exists. For hosted agents, also verify the targeted version is `active`.5960### Step 2: Fast smoke test for azd-deployed agents6162When the current folder is an azd agent project and deployment just completed, prefer the azd CLI first:6364```bash65azd ai agent invoke "hello, are you up?"66```6768Use `azd ai agent show --output json` only when you need structured status, version, endpoints, or troubleshooting details; a successful remote invocation is the fast smoke test.6970If `azd ai agent invoke` returns a `confirmation_required` envelope, summarize the change and proceed only when the user already requested remote invocation or explicitly consents. Prefer the returned `confirmCommand` over inventing flags. If azd cannot resolve the service or agent name, fall back to the MCP workflow below with the resolved `projectEndpoint` and `agentName`.7172For a post-deploy smoke test, invoke once unless the user explicitly asked to validate multi-turn/session behavior. If that single invoke returns a successful response, the smoke test passes;7374### Step 3: Create Session (Hosted Agents)7576For hosted agents, create a session before invoking using `session_create` with `projectEndpoint` and `agentName`. Optionally provide a `sessionId` (must match `^[A-Za-z0-9_-]{8,128}$`). Store the returned `sessionId` for subsequent calls.7778> ⚠️ Skip this step for prompt agents — they do not use sessions.7980For full session lifecycle details, see [Session Management](references/session-management.md).8182### Step 4: Invoke Agent8384Use the project endpoint and agent name from the project context. Use `agent_invoke` with:85- `projectEndpoint`, `agentName`, `inputText` (required)86- `agentVersion`, `conversationId`, `sessionId`, `protocol`, `stream` (optional)8788**Responses protocol** (default): `inputText` is a natural language message string. Multi-turn via `conversationId`.8990**Invocations protocol**: Set `protocol: 'invocations'`. This is **bytes in, bytes out** — `inputText` is forwarded as the raw HTTP request body to the container. The developer defines the expected schema.9192> ⚠️ **Do not guess the invocations request body.** To discover the expected schema:93> 1. **Fetch the OpenAPI spec**: `GET {projectEndpoint}/agents/{agentName}/endpoint/protocols/invocations/docs/openapi.json` (if the developer registered one)94> 2. Inspect the agent's **route handler code** or README for the expected payload shape95> 3. If unknown, ask the user for the agent's API contract before invoking9697Example invocations call (agent expects `{"message": "<text>"}`):9899```text100agent_invoke(projectEndpoint, agentName, inputText: "{\"message\":\"hello\"}", protocol: "invocations", sessionId: "<id>")101```102103See [Invocations Protocol Guide](references/invocations-protocol.md) for full details and examples.104105### Step 5: Multi-Turn Conversations106107**Responses protocol** → Pass `conversationId` from previous response to continue the thread. Platform manages history.108109**Invocations protocol** → Reuse same `sessionId`; conversation state is agent-managed via `$HOME`. Do **not** pass `conversationId` — it has no effect for invocations.110111### Step 6: File Operations (Hosted Agents)112113Upload/download files to pass data to and retrieve results from agents. All file operations require an active session. See [File Operations](references/file-operations.md).114115### Step 7: Clean Up116117Use `session_delete` to release compute resources when done. Undeleted sessions expire per platform policies.118119## Agent Type Differences120121| Behavior | Prompt Agent | Hosted Agent |122|----------|--------------|--------------|123| Readiness | Immediate | After deployment, version must be `active` |124| Session | Not applicable | Required via `session_create` |125| Multi-turn | Via `conversationId` | Via `conversationId` (responses) or session state (invocations) |126| File operations | ❌ | ✅ via session file tools |127| Protocol | `responses` only | `responses` or `invocations` |128129## Error Handling130131| Error | Cause | Resolution |132|-------|-------|------------|133| Agent not found | Invalid name or endpoint | Use `agent_get` to list agents |134| Hosted agent not active | Version still provisioning or failed | Check version status via `agent_get` |135| Session not found | Invalid ID or expired | Create new session with `session_create` |136| `424 FailedDependency` or `session_not_ready` | Hosted agent session is still warming up or readiness has not completed | Wait 15-30 seconds, check `session_logstream` if needed, then retry `agent_invoke` with the same `sessionId` if one was returned; if no `sessionId` was returned, retry `session_create`. If this persists across 3+ retries (with exponential backoff: 15s, 30s, 60s), the container likely cannot start within the readiness probe deadline — redeploy with higher CPU/memory (recommended minimum: `1` CPU / `2Gi` for direct-code deployments). Also verify the model deployment name is correct via `model_deployment_get`. |137| `could not resolve agent service in azd project: no azure.ai.agent service named '<agentName>' found in azure.yaml` from `azd ai agent invoke` | Name mismatch. | Update the agent name to the deployed agent name. |138| `invalid value "json" for --output` from `azd ai agent invoke` | Invoke supports only `default` and `raw` currently. | Retry without `--output json`. |139| Invocation failed | Model error, timeout, or invalid input | Check agent logs, verify model deployment |140| Invocations schema mismatch | Request body does not match what the agent expects | Inspect agent's route handler or API docs for the correct JSON schema; do not guess |141| File operation failed | Session not active or invalid path | Verify session with `session_get` |142| Permission error | Missing RBAC | Follow [troubleshoot skill](../troubleshoot/troubleshoot.md) |143| Rate limit exceeded | Too many requests | Implement backoff and retry |144145## Additional Resources146147- [Session Management](references/session-management.md)148- [File Operations](references/file-operations.md)149- [Foundry Hosted Agents](https://learn.microsoft.com/en-us/azure/ai-foundry/agents/concepts/hosted-agents?view=foundry)150- [Foundry Samples](https://github.com/azure-ai-foundry/foundry-samples)151