Skip to main content
Glama

Flatten messages[] (in memory)

flatten_messages
Read-onlyIdempotent

Reduce token usage by replacing large tool_result blocks in messages with compact markers, returning extracted data for separate storage and later restoration.

Instructions

Flatten a raw Anthropic Messages API messages[] array in memory: every bulky tool_result block (large text or base64 image) larger than min_size bytes is swapped for a compact [FLATTENED id=...] marker, and the originals are returned in "extracted". Persist "extracted" yourself — you are the store — and feed it back to unflatten_messages to restore the conversation byte-for-byte. Purely functional: no session file, no disk, no network; the input is never mutated. This is the same engine as the flatten-mcp library export; for production use inside your own process, prefer importing the library so the conversation does not travel over a transport.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
messagesYesThe raw Messages API messages[] array ({ role, content } objects, verbatim).
min_sizeNoOnly flatten tool_result blocks larger than N serialized bytes.

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
messagesYesThe flattened messages[] array — safe to send — with every bulky tool_result replaced by a compact [FLATTENED id=...] marker. Deep-copied; the input is never mutated.
extractedYesThe originals removed from the conversation. Persist this array yourself and pass it back to unflatten_messages to restore byte-for-byte.
flattenedCountYesHow many tool_result blocks were flattened.
contextTokensExactYesAlways false here — the in-memory tool never makes a network call; use the library flattenMessagesExact for an exact count.
contextTokensSavedYesEstimated context tokens removed from the conversation.
imageBlocksFlattenedYesHow many image blocks were among the flattened results.
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Beyond annotations (readOnlyHint, idempotentHint), the description adds that it is purely functional with no session file, disk, or network, and the input is never mutated. This enriches behavioral understanding.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is front-loaded with the action and each sentence adds value. While somewhat long, it is well-structured and informative without redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the output schema exists, the description appropriately focuses on usage and behavior. It covers the functional nature, persistence requirement, and alternative, making it complete for the tool's complexity.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, but the description adds meaning: it explains min_size is for blocks larger than N bytes, and that messages should be a raw Messages API array. This adds value beyond the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states it flattens a raw Anthropic Messages API messages[] array in memory, swapping bulky tool_result blocks with markers and returning originals in 'extracted'. It uses a specific verb and resource, and distinguishes from siblings like unflatten_messages.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description tells the agent to persist 'extracted' itself and feed it back to unflatten_messages for restoration. It also advises using the library directly for production, providing clear context on when to use this tool vs alternatives.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/shayaShav/flatten-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server