How do you handle different content types when reading MCP resources?
Accessing MCP Resources from a Client
Audio flashcard · 0:28Nortren·
How do you handle different content types when reading MCP resources?
0:28
Your read resource function checks the MIME type to decide how to process the content. If the MIME type is application slash json, you parse the text as JSON and return the parsed Python object, so the rest of your code can work with a real dictionary or list. Otherwise, you return the raw text content as it is. This simple branching handles both structured data, like a list of documents, and plain text documents seamlessly. If you later need to handle binary data or another structured format, you just add another branch to the same function.
modelcontextprotocol.io