How to return csv format for some tools #971
Replies: 2 comments 12 replies
|
I would suggest returning as string and doing the conversion in your tool. With the current spec, clients have poor to no support for non-json structured output. It's perfectly valid to use the "old" string-based content mechanism. It is not deprecated or legacy tool format. It gives you full control of what is sent to the model. For a csv returning tool I think it's the right choice. |
|
The existing answer is the safest default: CSV is text, so return the CSV string (or a If you need one centralized policy, current 2.x has a real post-processing point: an ordinary builder.Services.AddMcpServer()
.WithRequestFilters(filters =>
{
filters.AddCallToolFilter(next => async (context, cancellationToken) =>
{
CallToolResult result = await next(context, cancellationToken);
if (result.IsError is not true &&
context.Params.Name is "export_orders" or "export_customers" &&
result.StructuredContent is { } value)
{
result.Content =
[
new TextContentBlock
{
Text = CsvSerializer.Serialize(value)
}
];
}
return result;
});
})
.WithTools<ExportTools>();The relevant tools need [McpServerTool(UseStructuredContent = true)]
public static IReadOnlyList<OrderRow> ExportOrders() => GetRows();Keeping If the client specifically understands embedded resources, another valid representation is an Also, CSV conversion is not fully generic: column order, nested objects, arrays, quoting, culture, and formula-injection handling require an explicit contract. I would centralize the filter and serializer, but still explicitly allowlist the tools whose output schema is genuinely tabular rather than converting every tool response globally. Current SDK references: |
Uh oh!
There was an error while loading. Please reload this page.
Pre-submission Checklist
Question Category
Your Question
When the tool return complex type, by default it return in json format, how to return the data in csv format? Before i make each converter on each tool or is there centralised place to it like CallToolHandler?
All reactions