Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

resources.test.ts

50 lines1.7 KB

1import { describe, expect, it } from 'vitest'
2import { createMCPClientFromTransport } from '../src/client'
3import { mcpResourceToContentPart } from '../src/resources'
4import { makeServerWithResource } from './helpers/in-memory-server'
5​
6describe('mcpResourceToContentPart', () => {
7 it('converts a text content block to a TextPart', () => {
8 const part = mcpResourceToContentPart({ uri: 'file:///x', text: 'hello' })
9 expect(part.type).toBe('text')
10 expect((part as { type: 'text'; content: string }).content).toBe('hello')
11 })
12​
13 it('converts a blob content block to a TextPart with binary placeholder', () => {
14 const part = mcpResourceToContentPart({
15 uri: 'file:///img.png',
16 blob: 'abc123',
17 })
18 expect(part.type).toBe('text')
19 expect((part as { type: 'text'; content: string }).content).toBe(
20 '[binary resource file:///img.png]',
21 )
22 })
23​
24 it('falls back to JSON.stringify for unknown content', () => {
25 const input = {
26 uri: 'file:///unknown',
27 mimeType: 'application/octet-stream',
28 }
29 const part = mcpResourceToContentPart(input)
30 expect(part.type).toBe('text')
31 expect((part as { type: 'text'; content: string }).content).toBe(
32 JSON.stringify(input),
33 )
34 })
35})
36​
37describe('MCPClient resource methods (connected)', () => {
38 it('resources() / readResource() round-trip via in-memory server', async () => {
39 await using client = await createMCPClientFromTransport(
40 (await makeServerWithResource()).clientTransport,
41 )
42​
43 const list = await client.resources()
44 expect(list.length).toBeGreaterThan(0)
45​
46 const read = await client.readResource(list[0]!.uri)
47 const part = mcpResourceToContentPart(read.contents[0]!)
48 expect(part.type).toBe('text')
49 })
50})