I spend a lot of time testing and debugging APIs, and so do my AI agents. Lately I notice that my agents lose time and tokens on it. They look for the right port, guess at endpoints, fight with a terminal, and try to get the database back into a known state. Every run starts with the same detective work.
So for this series of posts I picked one scenario and I’m sticking with it: the API-testing loop. The goal is to adapt Aspire so that I (and my agents) can debug and test easily and efficiently.
Part 1 gave me an export button for the OpenAPI spec. Now the next step in the loop: firing a test call at an endpoint. To check one endpoint I open a REST client, look up the port and type the URL. Again. My agents do the same, only they pay for it in tokens.
Note: this post is part of a series about the demos from my session “Bending Aspire to your will”.
The test call I want to make is simple: retrieve the products of the catalog service for a given brand. The endpoint is /api/v1/catalog/items/type/all/brand/{brandId}. What I want is a command that asks for the brand ID, calls the endpoint and shows me the JSON.
A command that returns a result
Part 1 used WithProcessCommand to wrap curl. This time I don't need an external process. WithCommand lets you run your own C# code and return a result to the caller:
var catalogEndpoint = catalogService.GetEndpoint("http");
catalogService.WithCommand(
name: "retrieve-products",
displayName: "Retrieve products",
executeCommand: async context =>
{
var brandId = context.Arguments.GetInt32("brandId");
var path = $"/api/v1/catalog/items/type/all/brand/{brandId.ToString(CultureInfo.InvariantCulture)}";
try
{
using var client = new HttpClient();
using var response = await client.GetAsync(new Uri(new Uri(catalogEndpoint.Url), path), context.CancellationToken);
if (!response.IsSuccessStatusCode)
{
return CommandResults.Failure($"Catalog service returned HTTP {(int)response.StatusCode} ({response.ReasonPhrase}).");
}
var json = await response.Content.ReadAsStringAsync(context.CancellationToken);
return CommandResults.Success(
message: "Products retrieved for brand " + brandId + ".",
result: json,
resultFormat: CommandResultFormat.Json);
}
catch (HttpRequestException ex)
{
context.Logger.LogError(ex, "Failed to retrieve products for brand {BrandId}.", brandId);
return CommandResults.Failure($"Failed to retrieve products for brand {brandId}: {ex.Message}");
}
catch (OperationCanceledException) when (!context.CancellationToken.IsCancellationRequested)
{
return CommandResults.Failure($"The catalog service request for brand {brandId} timed out.");
}
},
// commandOptions: see next section
);
Three things matter here:
- Forward the
CancellationToken. If someone cancels the command, the HTTP call stops too. - Fail with a message a human can act on.
CommandResults.Failureshows up in the dashboard. "Catalog service returned HTTP 404 (Not Found)" tells you more than a generic error. - Return a structured result. With
CommandResultFormat.Jsonthe dashboard shows the response as formatted JSON, and callers like the CLI or an agent get it as data.
Remark: I create a new HttpClient per call. For a demo that's fine. In a long-running AppHost you may want to reuse one.
Ask for input and validate it
The command needs a brand ID. You declare that as an input on the command options:
commandOptions: new CommandOptions
{
Arguments =
[
new InteractionInput { Name = "brandId", Label = "Brand ID", InputType = InputType.Number, Required = true },
],
ValidateArguments = context =>
{
var brandId = context.Inputs.GetInt32("brandId");
if (brandId <= 0)
{
context.AddValidationError("brandId", "Brand ID must be a positive number.");
}
return Task.CompletedTask;
}
});
When you click the button, the dashboard shows a form with a number field. ValidateArguments runs before the command executes, so a brand ID of 0 is rejected without a single HTTP call.
One command, three front doors
The nice part is that I only wrote this once. The same command is available from:
- The dashboard: the "Retrieve products" button on the catalog service
- The CLI:
aspire resource <resource> <command> - MCP: the
execute_resource_commandtool of the Aspire MCP server
That last one is why I'm doing this. An agent no longer has to find the port, guess the URL or write a curl call. It sees a command with a name and a typed input, and it gets JSON back. It's less guessing, so fewer tokens.
Remark: the dashboard asks for input through a form. On the command line and via MCP, the arguments have to be supplied by the caller. Not every interaction is available on every surface, so test the command in each of them.
That's it! The test call lives in the AppHost now, next to the resource it belongs to. In the next part, I'll tackle the third step of the loop: resetting the state so every test run starts clean.





