# HostedAgentResourceBuilderExtensions Methods

- Package: [Aspire.Hosting.Foundry](/reference/api/csharp/aspire.hosting.foundry.md)
- Type: [HostedAgentResourceBuilderExtensions](/reference/api/csharp/aspire.hosting.foundry/hostedagentresourcebuilderextensions.md)
- Kind: `Methods`
- Members: `3`

Extension methods for adding hosted agent applications to the distributed application model.

## AsHostedAgent(IResourceBuilder<T>)

- Name: `AsHostedAgent(IResourceBuilder<T>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/becb48e2d61099e35ae336d527d3875e928d6594/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L45)

Configures the resource to run locally as a Microsoft Foundry hosted agent.

```csharp
public static class HostedAgentResourceBuilderExtensions
{
    public static IResourceBuilder<T> AsHostedAgent<T>(
        this IResourceBuilder<T> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The resource builder for the compute resource.

## Returns

`IResourceBuilder<T>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## Remarks

This method applies in run mode. It configures the resource with the hosted agent responses endpoint, a dashboard command for sending messages to the agent, and OpenTelemetry environment variables expected by the Microsoft Foundry agent server SDK.

## Examples

```csharp
var agent = builder.AddProject<Projects.AgentService>("agent")
    .AsHostedAgent();
```

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## AsHostedAgent(IResourceBuilder<T>, IResourceBuilder<AzureCognitiveServicesProjectResource>, Action<HostedAgentConfiguration>)

- Name: `AsHostedAgent(IResourceBuilder<T>, IResourceBuilder<AzureCognitiveServicesProjectResource>, Action<HostedAgentConfiguration>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/becb48e2d61099e35ae336d527d3875e928d6594/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L98)

Configures the resource to run and publish as a hosted agent in Microsoft Foundry, with full programmatic access to the underlying [HostedAgentConfiguration](/reference/api/csharp/aspire.hosting.foundry/hostedagentconfiguration.md) (including Azure SDK-specific options such as tools and content filters).

```csharp
public static class HostedAgentResourceBuilderExtensions
{
    public static IResourceBuilder<T> AsHostedAgent<T>(
        this IResourceBuilder<T> builder,
        IResourceBuilder<AzureCognitiveServicesProjectResource>? project,
        Action<HostedAgentConfiguration>? configure = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The resource builder for the compute resource.
- `project` (`IResourceBuilder<AzureCognitiveServicesProjectResource>`)
  Optional Microsoft Foundry project resource used for both run and publish mode configuration. When `null`, an existing Foundry project in the model is reused or a new project is created in publish mode.
- `configure` (`Action<HostedAgentConfiguration>`) `optional`
  A callback to configure hosted agent deployment options.

## Returns

`IResourceBuilder<T>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## Remarks

The [HostedAgentConfiguration.ContainerProtocolVersions](/reference/api/csharp/aspire.hosting.foundry/hostedagentconfiguration/properties.md#containerprotocolversions) setting affects both run and publish mode. Other settings are used in publish mode.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## AsHostedAgent(IResourceBuilder<T>, Action<HostedAgentConfiguration>)

- Name: `AsHostedAgent(IResourceBuilder<T>, Action<HostedAgentConfiguration>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/becb48e2d61099e35ae336d527d3875e928d6594/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L142-L143)

Configures the resource to run and publish as a hosted agent in Microsoft Foundry, with full programmatic access to the underlying [HostedAgentConfiguration](/reference/api/csharp/aspire.hosting.foundry/hostedagentconfiguration.md). The Foundry project is resolved automatically in publish mode.

```csharp
public static class HostedAgentResourceBuilderExtensions
{
    public static IResourceBuilder<T> AsHostedAgent<T>(
        this IResourceBuilder<T> builder,
        Action<HostedAgentConfiguration> configure)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The resource builder for the compute resource.
- `configure` (`Action<HostedAgentConfiguration>`)
  A callback to configure hosted agent deployment options.

## Returns

`IResourceBuilder<T>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.
