contentPartToMcp function

JsonMap? contentPartToMcp(
  1. ContentPart part
)

Converts a ContentPart into an MCP content block.

Returns null for parts that have no MCP equivalent — a tool call or a tool result, which are envelope concerns in MCP rather than content.

Implementation

JsonMap? contentPartToMcp(ContentPart part) => switch (part) {
  TextPart(:final text) => <String, Object?>{'type': 'text', 'text': text},
  // Reasoning is deliberately rendered as text rather than dropped: a server
  // that asked for a model's answer wants the answer, and a client that hides
  // the model's own explanation of it is answering a different question.
  ReasoningPart(:final text) => <String, Object?>{'type': 'text', 'text': text},
  ImagePart(:final mimeType) => <String, Object?>{
    'type': 'image',
    'data': _base64Of(part),
    'mimeType': mimeType,
  },
  AudioPart(:final mimeType) => <String, Object?>{
    'type': 'audio',
    'data': _base64Of(part),
    'mimeType': mimeType,
  },
  FilePart(:final mimeType, :final name) => <String, Object?>{
    'type': 'resource',
    'resource': pruneNulls(<String, Object?>{
      'uri': part.uri?.toString() ?? 'file:///${name ?? 'attachment'}',
      'mimeType': mimeType,
      'blob': _base64Of(part),
    }),
  },
  ToolCallPart() || ToolResultPart() => null,
};