Jina AI MCP Server

  • build
#!/usr/bin/env node import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } from '@modelcontextprotocol/sdk/types.js'; import axios from 'axios'; import dotenv from 'dotenv'; dotenv.config(); const JINA_API_KEY = process.env.JINA_API_KEY; if (!JINA_API_KEY) { throw new Error('JINA_API_KEY environment variable is required'); } class JinaServer { constructor() { this.server = new Server({ name: 'jina-ai-server', version: '0.1.0', }, { capabilities: { tools: {}, }, }); this.axiosInstance = axios.create({ baseURL: 'https://api.jina.ai/v1', headers: { 'Authorization': `Bearer ${JINA_API_KEY}`, 'Content-Type': 'application/json', }, }); this.setupToolHandlers(); this.server.onerror = (error) => console.error('[MCP Error]', error); process.on('SIGINT', async () => { await this.server.close(); process.exit(0); }); } setupToolHandlers() { this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: 'semantic_search', description: 'Perform semantic/neural search on text documents', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query text', }, collection: { type: 'string', description: 'Collection name to search in', }, limit: { type: 'number', description: 'Maximum number of results', default: 10, }, }, required: ['query', 'collection'], }, }, { name: 'image_search', description: 'Search for similar images using an image URL', inputSchema: { type: 'object', properties: { imageUrl: { type: 'string', description: 'URL of the query image', }, collection: { type: 'string', description: 'Collection name to search in', }, limit: { type: 'number', description: 'Maximum number of results', default: 10, }, }, required: ['imageUrl', 'collection'], }, }, { name: 'cross_modal_search', description: 'Perform text-to-image or image-to-text search', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Text query or image URL', }, mode: { type: 'string', enum: ['text2image', 'image2text'], description: 'Search mode', }, collection: { type: 'string', description: 'Collection name to search in', }, limit: { type: 'number', description: 'Maximum number of results', default: 10, }, }, required: ['query', 'mode', 'collection'], }, }, ], })); this.server.setRequestHandler(CallToolRequestSchema, async (request) => { try { const args = request.params.arguments || {}; switch (request.params.name) { case 'semantic_search': { const searchArgs = this.validateSearchArgs(args); return await this.handleSemanticSearch(searchArgs); } case 'image_search': { const imageArgs = this.validateImageSearchArgs(args); return await this.handleImageSearch(imageArgs); } case 'cross_modal_search': { const crossModalArgs = this.validateCrossModalSearchArgs(args); return await this.handleCrossModalSearch(crossModalArgs); } default: throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}`); } } catch (error) { const err = error; throw new McpError(ErrorCode.InternalError, `Jina API error: ${err.response?.data?.message || err.message}`); } }); } validateSearchArgs(args) { if (typeof args.query !== 'string' || typeof args.collection !== 'string') { throw new McpError(ErrorCode.InvalidParams, 'Invalid semantic search arguments'); } return { query: args.query, collection: args.collection, limit: typeof args.limit === 'number' ? args.limit : undefined, }; } validateImageSearchArgs(args) { if (typeof args.imageUrl !== 'string' || typeof args.collection !== 'string') { throw new McpError(ErrorCode.InvalidParams, 'Invalid image search arguments'); } return { imageUrl: args.imageUrl, collection: args.collection, limit: typeof args.limit === 'number' ? args.limit : undefined, }; } validateCrossModalSearchArgs(args) { if (typeof args.query !== 'string' || typeof args.collection !== 'string' || (args.mode !== 'text2image' && args.mode !== 'image2text')) { throw new McpError(ErrorCode.InvalidParams, 'Invalid cross-modal search arguments'); } return { query: args.query, mode: args.mode, collection: args.collection, limit: typeof args.limit === 'number' ? args.limit : undefined, }; } async handleSemanticSearch(args) { const response = await this.axiosInstance.post(`/collections/${args.collection}/search`, { query: args.query, limit: args.limit || 10, type: 'text', }); return { content: [ { type: 'text', text: JSON.stringify(response.data.results, null, 2), }, ], }; } async handleImageSearch(args) { const response = await this.axiosInstance.post(`/collections/${args.collection}/search`, { query: args.imageUrl, limit: args.limit || 10, type: 'image', }); return { content: [ { type: 'text', text: JSON.stringify(response.data.results, null, 2), }, ], }; } async handleCrossModalSearch(args) { const response = await this.axiosInstance.post(`/collections/${args.collection}/search`, { query: args.query, limit: args.limit || 10, type: args.mode === 'text2image' ? 'text2image' : 'image2text', }); return { content: [ { type: 'text', text: JSON.stringify(response.data.results, null, 2), }, ], }; } async run() { const transport = new StdioServerTransport(); await this.server.connect(transport); console.error('Jina AI MCP server running on stdio'); } } const server = new JinaServer(); server.run().catch(console.error);