Skip to main content
Glama
wlmwwx

Jina AI Remote MCP Server

by wlmwwx

sort_by_relevance

Sort documents by relevance to a specific query using Jina AI's reranking technology for improved information retrieval and content filtering.

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

  • Handler function that executes the sort_by_relevance tool by calling the Jina Reranker API to rerank provided documents based on the query, returning the top relevant results.
    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) {
    			throw new Error("No documents provided for reranking");
    		}
    
    		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 each result as individual text items for consistency
    		const contentItems: Array<{ type: 'text'; text: string }> = [];
    
    		if (data.results && Array.isArray(data.results)) {
    			for (const result of data.results) {
    				contentItems.push({
    					type: "text" as const,
    					text: yamlStringify(result),
    				});
    			}
    		}
    
    		return {
    			content: contentItems,
    		};
    	} catch (error) {
    		return createErrorResponse(`Error: ${error instanceof Error ? error.message : String(error)}`);
    	}
    },
  • Zod schema defining the input parameters for the sort_by_relevance tool: query (string), documents (array of strings), top_n (optional number).
    {
    	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")
    },
  • Registration of the sort_by_relevance tool via server.tool call within registerJinaTools function, specifying name, description, input schema, and handler.
    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.",
    	{
    		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")
    	},
    	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) {
    				throw new Error("No documents provided for reranking");
    			}
    
    			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 each result as individual text items for consistency
    			const contentItems: Array<{ type: 'text'; text: string }> = [];
    
    			if (data.results && Array.isArray(data.results)) {
    				for (const result of data.results) {
    					contentItems.push({
    						type: "text" as const,
    						text: yamlStringify(result),
    					});
    				}
    			}
    
    			return {
    				content: contentItems,
    			};
    		} catch (error) {
    			return createErrorResponse(`Error: ${error instanceof Error ? error.message : String(error)}`);
    		}
    	},
    );
  • src/index.ts:21-21 (registration)
    Call to registerJinaTools which registers all Jina tools including sort_by_relevance during server initialization.
    registerJinaTools(this.server, () => this.props);
Behavior3/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It explains the core functionality (reranking by relevance) and mentions the external API (Jina Reranker), but does not disclose important behavioral traits such as rate limits, authentication requirements, error handling, or what happens when documents are empty. It adds some context about use cases but lacks operational details.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

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

The description is appropriately sized at three sentences, with the first sentence clearly stating the core functionality. Each sentence adds value: the first explains what the tool does, the second provides usage context, and the third gives application examples. There is minimal redundancy, though the second sentence could be slightly more concise.

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 output schema, no annotations), the description is somewhat complete but has gaps. It explains the purpose and usage context well but lacks details about behavioral aspects (e.g., performance, limitations, error cases) and output format. Without annotations or an output schema, the description should provide more operational context for effective use.

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 three parameters thoroughly. The description does not add any parameter-specific information beyond what's in the schema (e.g., it doesn't explain parameter interactions, default values, or constraints). The baseline score of 3 is appropriate when 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 tool's purpose with specific verbs ('rerank', 'sort') and resources ('list of documents', 'documents'), explicitly mentioning the Jina Reranker API. It distinguishes this tool from sibling tools like deduplicate_strings or search_web by focusing on relevance ranking rather than deduplication or web searching.

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

Usage Guidelines4/5

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

The description provides clear context for when to use the tool ('when you have multiple documents and want to sort them by how well they match a specific query or topic') and gives examples of use cases ('document retrieval, content filtering, or finding the most relevant information from a collection'). However, it does not explicitly state when NOT to use it or name specific alternatives among sibling tools.

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/wlmwwx/jina-mcp'

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