这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export type Photo = {
author?: Author
}

export type PhotoWithData = Photo & {
base64: string
}

export type Shop = {
id: string
name?: string
Expand Down Expand Up @@ -192,3 +196,44 @@ const arrayBufferToJSON = (arrayBuffer: ArrayBuffer) => {
return arrayBuffer
}
}

export const getShopPhotosWithData = async (
shopId: string,
options: Options
): Promise<PhotoWithData[]> => {
const shop = await getShop(shopId, options)

const photos = await Promise.all(
shop.photos?.map(async (photo): Promise<PhotoWithData | null> => {
try {
const buffer = await getContentFromKVAsset(
`shops/${shopId}/${photo.name}`,
{
namespace: options.c.env
? options.c.env.__STATIC_CONTENT
: undefined,
}
)
const base64 = arrayBufferToBase64(buffer)
return {
...photo,
base64,
}
} catch (e) {
console.error(`Failed to load image ${photo.name}:`, e)
return null
}
}) || []
)

return photos.filter(Boolean)
}

export const arrayBufferToBase64 = (arrayBuffer: ArrayBuffer): string => {
const bytes = new Uint8Array(arrayBuffer)
let binary = ''
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i])
}
return btoa(binary)
}
17 changes: 16 additions & 1 deletion src/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Context } from 'hono'
import { Hono } from 'hono'
import { z } from 'zod'
import type { Env } from './app'
import { getShop, listShopsWithPager } from './app'
import { getShop, getShopPhotosWithData, listShopsWithPager } from './app'

export const getMcpServer = async (c: Context<Env>) => {
const server = new McpServer({
Expand Down Expand Up @@ -49,6 +49,21 @@ export const getMcpServer = async (c: Context<Env>) => {
}
}
)
server.tool(
'get_photos_with_data',
'Get ramen photos with base64 data',
{ shopId: z.string() },
async ({ shopId }) => {
const photos = await getShopPhotosWithData(shopId, { c })
return {
content: photos.map((photo) => ({
type: 'image',
data: photo.base64,
mimeType: 'image/jpeg',
})),
}
}
)
return server
}

Expand Down
18 changes: 18 additions & 0 deletions test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
findIndexFromId,
getAuthor,
listShopsWithPager,
getShopPhotosWithData,
BASE_URL,
} from '@/app'

Expand Down Expand Up @@ -103,3 +104,20 @@ describe('getAuthor', () => {
expect(author.url).toBe('https://github.com/yusukebe')
})
})

describe('getShopPhotosWithData', () => {
it('Should return photos with base64 data', async () => {
const photos = await getShopPhotosWithData('yoshimuraya', options)
expect(photos).not.toBeFalsy()
expect(photos.length).toBe(1)
expect(photos[0].name).toBe('yoshimuraya-001.jpg')
expect(photos[0].url).toBe(
`${BASE_URL}images/yoshimuraya/yoshimuraya-001.jpg`
)
expect(photos[0].base64).toBeDefined()
expect(typeof photos[0].base64).toBe('string')
expect(photos[0].width).toBe(1200)
expect(photos[0].height).toBe(900)
expect(photos[0].authorId).toBe('yusukebe')
})
})
36 changes: 36 additions & 0 deletions test/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,42 @@ describe('Test /mcp', () => {
expect(shop).toHaveProperty('photos')
expect(Array.isArray(shop.photos)).toBe(true)
})

it('Should execute get_photos_with_data tool and return image data', async () => {
const res = await app.request('/mcp', {
method: 'POST',
body: JSON.stringify({
jsonrpc: '2.0',
id: 5,
method: 'tools/call',
params: {
name: 'get_photos_with_data',
arguments: {
shopId: 'yoshimuraya',
},
},
}),
headers: {
'Content-Type': 'application/json',
Accept: 'application/json, text/event-stream',
},
})

const messages = await parseSSEJSONResponse(res)
const result = messages.find((m) => m.id === 5)

expect(res.status).toBe(200)
expect(result).toHaveProperty('result')
expect(result.result).toHaveProperty('content')
expect(Array.isArray(result.result.content)).toBe(true)
expect(result.result.content.length).toBe(1)

const content = result.result.content[0]
expect(content).toHaveProperty('type', 'image')
expect(content).toHaveProperty('data')
expect(content).toHaveProperty('mimeType', 'image/jpeg')
expect(typeof content.data).toBe('string')
})
})

export async function parseSSEJSONResponse(res: Response) {
Expand Down