MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / formatToolResultAsText

Function formatToolResultAsText

packages/mcp-server/src/format.ts:156–186  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

154 * Returns null if the content is not suitable for text formatting.
155 */
156export function formatToolResultAsText(content: string): string | null {
157 let parsed: unknown
158 try {
159 parsed = JSON.parse(content)
160 } catch {
161 return null
162 }
163
164 if (!parsed || typeof parsed !== 'object') return null
165 const obj = parsed as Record<string, unknown>
166
167 // If there's a messages array with senderName/content/timestamp, format it
168 if (Array.isArray(obj.messages) && obj.messages.length > 0) {
169 const msgs = obj.messages as FormattableMessage[]
170 if (msgs[0] && 'senderName' in msgs[0] && 'timestamp' in msgs[0]) {
171 return formatObjectWithMessages(obj, msgs)
172 }
173 }
174
175 // If there are ranking/keywords arrays, format as numbered list
176 if (Array.isArray(obj.ranking) || Array.isArray(obj.keywords)) {
177 return formatArrayResult(obj)
178 }
179
180 // If there are sessions, format them compactly
181 if (Array.isArray(obj.sessions)) {
182 return formatSessionsList(obj)
183 }
184
185 return null
186}
187
188function formatObjectWithMessages(obj: Record<string, unknown>, msgs: FormattableMessage[]): string {
189 const lines: string[] = []

Callers 1

applyFormatFunction · 0.90

Calls 4

formatObjectWithMessagesFunction · 0.85
formatArrayResultFunction · 0.85
formatSessionsListFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected