Skip to main content
Glama
acchuang

Jina AI Remote MCP Server

by acchuang

sort_by_relevance

Sort documents by relevance to a specific query using Jina AI's reranking technology to prioritize the most matching content from a collection.

Instructions

Rerank a list of documents by relevance to a query using Jina Reranker API. Use this when you have multiple documents and want to sort them by how well they match a specific query or topic. Perfect for document retrieval, content filtering, or finding the most relevant information from a collection.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe query or topic to rank documents against (e.g., 'machine learning algorithms', 'climate change solutions')
documentsYesArray of document texts to rerank by relevance
top_nNoMaximum number of top results to return

Implementation Reference

  • Executes the tool logic by calling the Jina Reranker API (https://api.jina.ai/v1/rerank) with model 'jina-reranker-v2-base-multilingual' to rerank the provided documents by relevance to the query. Handles errors, token check, and formats output as YAML.
    async ({ query, documents, top_n }: { query: string; documents: string[]; top_n?: number }) => {
    	try {
    		const props = getProps();
    
    		const tokenError = checkBearerToken(props.bearerToken);
    		if (tokenError) {
    			return tokenError;
    		}
    
    		if (documents.length === 0) {
    			return {
    				content: [
    					{
    						type: "text" as const,
    						text: "No documents provided for reranking",
    					},
    				],
    				isError: true,
    			};
    		}
    
    		const response = await fetch('https://api.jina.ai/v1/rerank', {
    			method: 'POST',
    			headers: {
    				'Accept': 'application/json',
    				'Content-Type': 'application/json',
    				'Authorization': `Bearer ${props.bearerToken}`,
    			},
    			body: JSON.stringify({
    				model: 'jina-reranker-v2-base-multilingual',
    				query,
    				top_n: top_n || documents.length,
    				documents
    			}),
    		});
    
    		if (!response.ok) {
    			return handleApiError(response, "Document reranking");
    		}
    
    		const data = await response.json() as any;
    
    		return {
    			content: [
    				{
    					type: "text" as const,
    					text: yamlStringify(data.results),
    				},
    			],
    		};
    	} catch (error) {
    		return {
    			content: [
    				{
    					type: "text" as const,
    					text: `Error: ${error instanceof Error ? error.message : String(error)}`,
    				},
    			],
    			isError: true,
    		};
    	}
    },
  • Zod schema defining the input parameters for the tool: query (string), documents (array of strings), top_n (optional number). Used for validation.
    query: z.string().describe("The query or topic to rank documents against (e.g., 'machine learning algorithms', 'climate change solutions')"),
    documents: z.array(z.string()).describe("Array of document texts to rerank by relevance"),
    top_n: z.number().optional().describe("Maximum number of top results to return (default: all documents)")
  • Registers the 'sort_by_relevance' tool on the MCP server with name, description, input schema, and handler function. Called from src/index.ts via registerJinaTools.
    server.tool(
    	"sort_by_relevance",
    	"Rerank a list of documents by relevance to a query using Jina Reranker API. Use this when you have multiple documents and want to sort them by how well they match a specific query or topic. Perfect for document retrieval, content filtering, or finding the most relevant information from a collection. Returns documents sorted by relevance score.",
    	{
    		query: z.string().describe("The query or topic to rank documents against (e.g., 'machine learning algorithms', 'climate change solutions')"),
    		documents: z.array(z.string()).describe("Array of document texts to rerank by relevance"),
    		top_n: z.number().optional().describe("Maximum number of top results to return (default: all documents)")
    	},
    	async ({ query, documents, top_n }: { query: string; documents: string[]; top_n?: number }) => {
    		try {
    			const props = getProps();
    
    			const tokenError = checkBearerToken(props.bearerToken);
    			if (tokenError) {
    				return tokenError;
    			}
    
    			if (documents.length === 0) {
    				return {
    					content: [
    						{
    							type: "text" as const,
    							text: "No documents provided for reranking",
    						},
    					],
    					isError: true,
    				};
    			}
    
    			const response = await fetch('https://api.jina.ai/v1/rerank', {
    				method: 'POST',
    				headers: {
    					'Accept': 'application/json',
    					'Content-Type': 'application/json',
    					'Authorization': `Bearer ${props.bearerToken}`,
    				},
    				body: JSON.stringify({
    					model: 'jina-reranker-v2-base-multilingual',
    					query,
    					top_n: top_n || documents.length,
    					documents
    				}),
    			});
    
    			if (!response.ok) {
    				return handleApiError(response, "Document reranking");
    			}
    
    			const data = await response.json() as any;
    
    			return {
    				content: [
    					{
    						type: "text" as const,
    						text: yamlStringify(data.results),
    					},
    				],
    			};
    		} catch (error) {
    			return {
    				content: [
    					{
    						type: "text" as const,
    						text: `Error: ${error instanceof Error ? error.message : String(error)}`,
    					},
    				],
    				isError: true,
    			};
    		}
    	},
    );
Behavior3/5

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

No annotations are provided, so the description carries the full burden. It mentions the tool uses the 'Jina Reranker API' and describes its purpose, but lacks details on rate limits, authentication needs, error handling, or what the output looks like (since no output schema exists). The description is accurate but insufficient for full 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.

Conciseness5/5

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

The description is appropriately sized and front-loaded: the first sentence states the core purpose, followed by usage guidelines and examples. Every sentence adds value without redundancy, making it efficient and well-structured.

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

Completeness3/5

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

Given the tool's moderate complexity (3 parameters, no annotations, no output schema), the description is somewhat complete but has gaps. It explains the purpose and usage well but lacks details on behavioral aspects like output format, error cases, or API constraints, which are important for a tool with no annotations or output schema.

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

Parameters3/5

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

Schema description coverage is 100%, so the schema already documents all parameters thoroughly. The description doesn't add any additional meaning beyond what's in the schema (e.g., it doesn't explain parameter interactions or provide examples). Baseline score of 3 is appropriate as the schema does the heavy lifting.

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 the specific action ('Rerank a list of documents by relevance') and resource ('documents'), using the Jina Reranker API. It distinguishes this tool from siblings like 'deduplicate_strings' or 'search_web' by focusing on reranking rather than searching or deduplication.

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

Usage Guidelines5/5

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

Explicitly states when to use this tool: 'Use this when you have multiple documents and want to sort them by how well they match a specific query or topic.' It also provides alternative use cases ('Perfect for document retrieval, content filtering, or finding the most relevant information from a collection'), though it doesn't explicitly name sibling 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/acchuang/jina-mcp'

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