Skip to content

feature: Add Type() method to the Content interface #373

Open
@duaraghav8

Description

@duaraghav8

Problem Statement

I'm writing an MCP client that performs a Tool call.
It does not know upfront about the output format of the Tool - it could be Text, Image, Audio - anything.

This makes it hard for me to typecast the response into the appropriate object mcp.AsTextContent() or mcp.AsImageContent(), etc. because there is no way to know the type of the response content.

In python, it is easy because I can directly access the type field from the response object. eg-

tool_result = await session.call_tool("return_image")
print(tool_result.content[0].type)

But this is currently not possible in Go with mcp-go because the Content interface doesn't allow us to examine the type.

Proposed Solution

Ideally, I'd like to do something like:

callToolResp, _ := mcpClient.CallTool(ctx, callToolReq)

if callToolResp.Content[0].Type() == mcp.ContentTypeText {
    tc, _ := mcp.AsTextContent(callToolResp.Content[0])
} else if callToolResp.Content[0].Type() == mcp.ContentTypeImage {
    tc, _ := mcp.AsImageContent(callToolResp.Content[0])
}

So I propose to add the Type() method to the Content interface that returns a string or even better, if you can define constants like mcp.ContentTypeImage, etc.

MCP Spec Reference

https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-03-26/schema.ts#L699

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions