AI agents and MCP
Let AI agents draw charts in your .NET app: RioCharts.AI gives them a create_chart tool. The agent writes a small
JSON definition against your datasets; the tool validates it, renders it on the server and returns a short description,
or errors that name the path and the valid options so the model can fix the definition and call again. The image goes to your app;
render_chart returns it to the agent instead.
Install
dotnet add package RioCharts.AI --prereleaseIt brings in RioCharts.Skia for the images. For a local agent with no app of your own, use riocharts-mcp instead.
Your data
// Datasets the agent may chart: from rows, objects or JSON.
var shop = ChartDataset.FromItems("shop", traffic, "Store traffic per sales channel, hourly",
new Dictionary<string, string> { ["orders"] = "orders per hour", ["revenue"] = "revenue per hour, €" });
var source = new InMemoryDataSource([shop]); // or implement IChartDataSource to load on demand
var tool = new ChartTool(source, new ChartToolOptions { Export = new ExportOptions { Width = 720, Height = 360 } });
// Every chart the tools make (result.Tool says which: create_chart or render_chart).
tool.ChartCreated += result => { if (result.Png is { } png) ShowToUser(png, result.Summary); };The tool description lists each dataset with its fields, types and descriptions, the definition format and a few examples. Querying and access control stay in your app.
Microsoft.Extensions.AI
IChatClient client = ...;
var options = new ChatOptions { Tools = tool.AsAIFunctions() };
var response = await client.GetResponseAsync("Chart orders per channel this week, with our goal of 40 an hour", options);
// The model calls create_chart with e.g.
// {"dataset":"shop","series":[{"type":"line","x":"timestamp","y":"orders","groupBy":"channel"}],
// "annotations":[{"value":40,"label":"Goal"}]}MCP server
// dotnet add package ModelContextProtocol.AspNetCore
builder.Services.AddMcpServer()
.WithHttpTransport()
.WithRioChartsTools(tool);
app.MapMcp("/mcp");Tools: create_chart (returns text and a PNG image), render_chart (PNG or SVG for the agent), list_datasets and get_chart_schema.
Images for the agent
create_chart shows a chart to the user. When the agent needs the image itself, for a report, an email or a document,
it calls render_chart with the same definition plus format (png or svg), width,
height and theme. The image comes back as content: DataContent with Microsoft.Extensions.AI, an image block
(PNG) or an embedded image/svg+xml resource (SVG) over MCP.
// Optional: also save each image to a folder and return its path (for local agents).
var tool = new ChartTool(source, new ChartToolOptions { OutputDirectory = "charts" });
// The model calls render_chart with e.g.
// {"definition":{"dataset":"shop","title":"Revenue per day","series":[{"type":"bar","x":"timestamp","y":"revenue","timeBucket":"day","aggregate":"sum"}]},
// "format":"svg","width":640,"height":320}
// Or from code:
var result = await tool.RenderChartAsync(json, new ChartImageRequest { Format = ChartImageFormat.Png, Width = 640, Height = 320 });
byte[] png = result.Png!;Local agents: riocharts-mcp
For agents on your machine, such as Claude Code, Claude Desktop or GitHub Copilot in VS Code, there is a ready-made MCP server. It runs over stdio,
so the agent starts it; you write no host app. CSV and JSON files in the --data folder become datasets (read again when they change),
and every chart is saved to --out as PNG or SVG, with the path returned to the agent. Tools: render_chart,
list_datasets, get_chart_schema, and for agents writing app code get_razor and get_usage
(see coding agents). It runs on Windows (x64, arm64, x86), macOS and Linux (x64 and arm64, glibc and musl).
dotnet tool install -g RioCharts.Mcp --prerelease
# later, to get a newer version:
dotnet tool update -g RioCharts.Mcp --prerelease// .mcp.json (Claude Code), or the MCP settings of your agent
{
"mcpServers": {
"riocharts": { "command": "riocharts-mcp", "args": ["--data", "data", "--out", "charts"] }
}
}
Options: --data, --out (default ./charts), --width, --height,
--theme light|dark and --culture (e.g. sv-SE). CSV files may use comma, semicolon or tab, and a decimal comma
with semicolons, as Excel writes them.
Coding agents
A coding agent (Claude Code, Copilot, Cursor) that adds a chart to your app needs to know a library its model was not trained on. RioCharts gives it that in two ways:
llms.txtandllms-full.txton this site: the second is the whole guide in one Markdown file, with setup, rules, examples and every component parameter. It is generated from the code and its examples are compiled and rendered by the tests, so it matches the packages. Point the agent at it in yourAGENTS.mdorCLAUDE.md; Getting started has a block to paste.riocharts-mcphas two tools for it.get_usagereturns the same guide: the overview, or one component or type with atopic.get_razorturns a definition into code: the agent designs the chart withrender_chart, looks at the image, then gets the<RioChart>markup, a record for the rows (from the dataset's fields) and, for inline data, the rows as a C# list.
@* get_razor with {"data":[{"product":"Shoes","sold":45},{"product":"Bags","sold":12}],
"series":[{"type":"pie","x":"product","y":"sold"}]} returns: *@
public sealed record Row(string Product, double Sold);
List<Row> rows =
[
new("Shoes", 45),
new("Bags", 12),
];
<RioChart TItem="Row" Items="rows">
<PieSeries XValue="r => r.Product" YValue="r => r.Sold" />
</RioChart>From chat to code
// Turn a definition the agent built into component markup for the codebase:
string razor = ChartRazor.FromDefinition(ChartDefinitionJson.Parse(json).Definition!, itemType: "Traffic", itemsName: "traffic");