Skip to content

IActionResult is not compatible with MCP #1042

@javierhersan

Description

@javierhersan

If I return the following IActionResult, the MCP output when the agent executes it is {"result":{}}

  [HttpGet]
  [McpServerTool(Name = "GetTodos"), Description("Returns the list of todos.")]
  public ActionResult<Todo[]> GetTodos()
  {
      return Ok(todos);
  }

But if I return instead a plain object, the agent gets the full list of todos.

  [HttpGet]
  [McpServerTool(Name = "GetTodos"), Description("Returns the list of todos.")]
  public Todo[] GetTodos()
  {
      return todos;
  }

IActionResult support is important to manage HTTP result in our existing APIs.

The existing code is:

using Microsoft.AspNetCore;

/*
Using directive is unnecessary.IDE0005
namespace Microsoft.AspNetCore
*/

// var builder = WebApplication.CreateSlimBuilder(args);
var builder = WebApplication.CreateBuilder(args);

// builder.Services.ConfigureHttpJsonOptions(options =>
// {
    
// });

builder.Services.AddControllers();

builder.Services.AddMcpServer()
    .WithHttpTransport()
    .WithToolsFromAssembly();

// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();
app.MapMcp("/mcp");

app.Run();
using System.ComponentModel;
using Microsoft.AspNetCore.Mvc;
using ModelContextProtocol.Server;
namespace API.Controllers;

[ApiController]
[Route("api/[controller]")]
[McpServerToolType]
public class TodosController : ControllerBase
{
    private readonly Todo[] todos = [];
    public record Todo(int Id, string? Title, DateOnly? DueBy = null, bool IsComplete = false);

    public TodosController()
    {
        todos = [
            new(1, "Walk the dog"),
            new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)),
            new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))),
            new(4, "Clean the bathroom"),
            new(5, "Clean the car", DateOnly.FromDateTime(DateTime.Now.AddDays(2)))
        ];
    }

    [HttpGet]
    // [Route(nameof(GetTodos))]
    [McpServerTool(Name = "GetTodos"), Description("Returns the list of todos.")]
    public ActionResult<Todo[]> GetTodos()
    {
        return Ok(todos);
    }

    [HttpGet("{id}")]
    [McpServerTool(Name = "GetTodoById"), Description("Returns a todo by its ID.")]
    public ActionResult<Todo> GetTodoById(int id)
    {
        var todo = todos.FirstOrDefault(a => a.Id == id);
        return todo is not null ? Ok(todo) : NotFound();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions