Recursive AI

Serve a binary resource from a tool server in C#

OneModelContext is a free, open-source .NET SDK for the Model Context Protocol® from Recursive AI. MIT or Apache-2.0, no native dependencies.

Serve tile.png (in the zip) at a fixed resource URI so a client can read it by name.

Terminal
dotnet add package RecursiveAI.OneModelContext

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneModelContext;
using RecursiveAI.OneModelContext.Binding;
 
const string uri = "assets://demo/tile.png";
const string mime = "image/png";
var bytes = File.ReadAllBytes("tile.png");
var server = new ContextServer("assets", "1.0");
server.Resources.Add(
    new(uri, "Blue tile") { MimeType = mime },
    (id, _) => ValueTask.FromResult(
        StringArguments.BlobResource(id, mime, bytes)));
await ModelContext.ServeAsync(server);

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

Serves the 16 × 16 blue PNG over standard input and output. The snippet runs as a server and doesn’t print a console summary; the full program below includes the client that reads it.

StringArguments.BlobResource is in the RecursiveAI.OneModelContext.Binding namespace.

The full program

C#
using System.Reflection;
using RecursiveAI.OneModelContext;
using RecursiveAI.OneModelContext.Binding;
using RecursiveAI.OneModelContext.Protocol;
 
const string uri = "assets://demo/tile.png";
const string mime = "image/png";
var original = File.ReadAllBytes("tile.png");
if (args is ["--serve"])
{
    var server = new ContextServer("assets", "1.0");
    server.Resources.Add(new(uri, "Blue tile") { MimeType = mime },
        (id, _) => ValueTask.FromResult(
            StringArguments.BlobResource(id, mime, original)));
    await ModelContext.ServeAsync(server);
    return;
}
 
var dll = Assembly.GetExecutingAssembly().Location;
var command = $"dotnet \"{dll}\" --serve";
await using var client = await ModelContext.ConnectAsync(command);
var result = await client.ReadResourceAsync(uri);
if (result.Contents.Single() is not BlobResourceContents blob)
    throw new InvalidDataException("Expected a binary resource.");
 
var bytes = Convert.FromBase64String(blob.Blob);
if (blob.MimeType != mime || !bytes.SequenceEqual(original))
    throw new InvalidDataException("The MIME type or bytes did not match.");
 
Console.WriteLine($"{blob.Uri}: {blob.MimeType}");
Console.WriteLine($"Received {bytes.Length} bytes, identical to tile.png.");

dotnet run prints:

Text
assets://demo/tile.png: image/png
Received 82 bytes, identical to tile.png.

The client starts the server as a child process, reads the fixed URI, and compares the decoded bytes with tile.png. Both the bytes and MIME type must match for it to print the confirmation. A request for an unregistered URI returns a “Resource not found” protocol error.

Variations

Save the decoded response to disk. Add this after the checks in the full program:

C#
File.WriteAllBytes("received.png", bytes);

The resulting PNG has the same bytes as tile.png.


The short answer

Register a resource with a fixed URI, such as assets://demo/tile.png. In the handler, pass the raw bytes and image/png to StringArguments.BlobResource. The client calls ReadResourceAsync with that URI, reads the returned BlobResourceContents, and decodes Blob from Base64 back to bytes.

How do binary resources carry bytes and MIME types?

The resource specification represents a binary resource with a uri, a blob string containing Base64, and an optional mimeType. BlobResource does the Base64 encoding itself. Pass the original bytes, not an already encoded string or a data: URL. The helper uses application/octet-stream if you pass null for the MIME type; set image/png when callers should recognize a PNG.

Set the MIME type both on the resource definition and on the returned blob. The first is metadata for clients that discover the resource in a list. The second travels with the contents when a client reads it. In the sample, the client checks the latter. The MIME type describes the payload; the bytes still need to be a valid file of that type.

The URI identifies the resource on the protocol connection. It isn’t a web download URL or a path the client needs to open. The server selects the file in its handler, so you can later change how you store it without changing what clients request.

What’s the difference between a resource and a tool result?

Use a resource when a client knows or discovers an identifier and wants the data at that identifier, such as a fixed diagram used for context. It sends resources/read. Use a tool when the client needs to call an operation with arguments and receive its result, such as generating a preview from a supplied size. The protocol’s resource interaction model leaves the choice of how to present resources to the host application: serving a PNG doesn’t guarantee that the host displays it.

The example sends one small blob in one response. The handler reads the sample into memory before serving, and the client allocates another byte array when it decodes the reply. It is not a streaming file endpoint. For large assets, consider whether the client should fetch a separate download URL instead of carrying the whole file in a resource message.

All onemodelcontext recipes