MCP Metadata in Swagger (JSON-RPC)

This part shows how to expose MCP metadata in Swagger and retrieve metadata in JSON-RPC style.

Source post (from my Blogger website Coding Grounds):

1) Enable Swagger in Program.cs

public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);

    //more code

    //Add swagger support
    builder.Services.AddControllers();
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();

    // some more code

    app.UseSwagger();
    app.UseSwaggerUI();
}
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.6" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="9.0.6" />

2) Expose tools metadata via controller

We use IMcpClient here from ModelContextProtocol.Client namespace to get the metadata of our MCP tools. The data is in Json-RPC format.

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using ModelContextProtocol.Client;

namespace WeatherServer.Web.Http.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ToolsController : ControllerBase
    {
        private readonly IMcpClient _client;
        private readonly IOptions<ModelContextProtocol.Protocol.Implementation> _mcpServerOptions;

        public ToolsController(IMcpClient client, IOptions<ModelContextProtocol.Protocol.Implementation> mcpServerOptions)
        {
            _client = client;
            _mcpServerOptions = mcpServerOptions;
        }

        [HttpGet(Name = "Overview")]
        [Produces("application/json")]
        public async Task<IActionResult> GetOverview()
        {
            var rpcRequest = new
            {
                jsonrpc = "2.0",
                method = "tools/list",
                id = 1
            };

            var tools = await _client.ListToolsAsync();
            return Ok(new
            {
                ServerName = _mcpServerOptions.Value.Title,
                Version = _mcpServerOptions.Value.Version,
                Tools = tools
            });
        }
    }
}

The core JSON-RPC shape is:

{
  "jsonrpc": "2.0",
  "method": "tools/list",
  "id": 1
}

JSON-RPC/MCP references:

Screenshots

Swagger JSON-RPC metadata output:

MCP Swagger JSON-RPC metadata

JSON browser view of metadata document:

Here I used JsonCrack to watch Json online to watch the different metadata about the MCP tools.

MCP JSON browser metadata view

Key takeaways