Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions sdk/guides/agent-file-based.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: File-Based Agents
description: Define specialized sub-agents as simple Markdown files with YAML frontmatter — no Python code required.

Check warning on line 3 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L3

Did you really mean 'frontmatter'?
---

import RunExampleCode from "/sdk/shared-snippets/how-to-run-example.mdx";
Expand All @@ -13,7 +13,7 @@

## Agent File Format

An agent is a single `.md` file with YAML frontmatter and a Markdown body:

Check warning on line 16 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L16

Did you really mean 'frontmatter'?

```markdown icon="markdown"
---
Expand All @@ -40,9 +40,9 @@
Keep feedback concise and actionable. For each issue, suggest a fix.
```

The YAML frontmatter configures the agent. The Markdown body becomes the agent's system prompt.

Check warning on line 43 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L43

Did you really mean 'frontmatter'?

### Frontmatter Fields

Check warning on line 45 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L45

Did you really mean 'Frontmatter'?

| Field | Required | Default | Description |
|-------|----------|---------|-------------|
Expand Down Expand Up @@ -166,23 +166,23 @@
print(f"Registered {len(agent_names)} agents: {agent_names}")
```

This scans both project-level and user-level directories, deduplicates by name, and registers each agent as a delegate that can be spawned by the orchestrator.

Check warning on line 169 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L169

Did you really mean 'deduplicates'?

## Manual Loading

For more control, load and register agents explicitly:

```python icon="python" focus={3-6, 8-14}
from pathlib import Path

Check warning on line 176 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L176

Did you really mean 'pathlib'?

from openhands.sdk import load_agents_from_dir, register_agent, agent_definition_to_factory

# Load from a specific directory
agents_dir = Path("agents")

Check warning on line 181 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L181

Did you really mean 'agents_dir'?
agent_definitions = load_agents_from_dir(agents_dir)

Check warning on line 182 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L182

Did you really mean 'agent_definitions'?

# Register each agent
for agent_def in agent_definitions:

Check warning on line 185 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L185

Did you really mean 'agent_def'?

Check warning on line 185 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L185

Did you really mean 'agent_definitions'?
register_agent(
name=agent_def.name,
factory_func=agent_definition_to_factory(agent_def),
Expand Down Expand Up @@ -218,7 +218,7 @@
```

The factory:
- Maps tool names from the frontmatter to `Tool` objects

Check warning on line 221 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L221

Did you really mean 'frontmatter'?
- Appends the Markdown body to the parent system message via `AgentContext(system_message_suffix=...)`
- Respects the `model` field (`"inherit"` keeps the parent LLM; an explicit model name creates a copy)

Expand All @@ -229,8 +229,8 @@
```python icon="python" focus={3, 4}
from openhands.sdk.subagent import load_project_agents, load_user_agents

project_agents = load_project_agents("/path/to/project")

Check warning on line 232 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L232

Did you really mean 'project_agents'?
user_agents = load_user_agents() # scans ~/.agents/agents/ and ~/.openhands/agents/

Check warning on line 233 in sdk/guides/agent-file-based.mdx

View check run for this annotation

Mintlify / Mintlify Validation (allhandsai) - vale-spellcheck

sdk/guides/agent-file-based.mdx#L233

Did you really mean 'user_agents'?
```

## Using with Delegation
Expand Down Expand Up @@ -344,6 +344,34 @@

The `mcp_servers` field uses the same format as the [MCP configuration](/sdk/guides/mcp) — each key is a server name, and the value contains `command` and `args` for launching the server.

#### Environment Variable Resolution

All string values in MCP server configurations support `${VAR}` (and `$VAR`) environment variable references, which are resolved from `os.environ` at load time. This lets you forward secrets and dynamic paths without hard-coding them in Markdown:

```markdown icon="markdown"
---
name: api-agent
description: Agent with MCP server using environment-based secrets.
mcp_servers:
my-server:
command: ${PLUGIN_ROOT}/bin/server
args:
- --config
- ${PLUGIN_ROOT}/config.json
env:
API_KEY: ${MY_API_KEY}
remote:
type: http
url: ${API_BASE}/mcp
headers:
Authorization: Bearer ${AUTH_TOKEN}
---

An agent that connects to MCP servers configured via environment variables.
```

Environment variable resolution applies recursively to all string fields — `command`, `args`, `url`, `headers`, `env`, and any other string values in the server config. If a referenced variable is not set, the placeholder is left unchanged (e.g., `${NONEXISTENT_VAR}` stays as-is).

### Hooks

File-based agents can define [lifecycle hooks](/sdk/guides/hooks) that run at specific points during execution:
Expand Down
Loading