File size: 1,747 Bytes
cf6b8d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * Utility function to handle common API errors for Jina AI services
 * Returns a standardized error response object for MCP tools
 */
export function handleApiError(response: Response, context: string = "API request") {
	if (response.status === 401) {
		return {
			content: [
				{
					type: "text" as const,
					text: "Authentication failed. Please set your API key in the Jina AI MCP settings. You can get a free API key by visiting https://jina.ai and signing up for an account.",
				},
			],
			isError: true,
		};
	}
	if (response.status === 402) {
		return {
			content: [
				{
					type: "text" as const,
					text: "This key is out of quota. Please top up this key at https://jina.ai",
				},
			],
			isError: true,
		};
	}
	
	if (response.status === 429) {
		return {
			content: [
				{
					type: "text" as const,
					text: "Rate limit exceeded. Please upgrade your API key to get higher rate limits. Visit https://jina.ai to manage your subscription and increase your usage limits.",
				},
			],
			isError: true,
		};
	}
	
	// Default error message for other HTTP errors
	return {
		content: [
			{
				type: "text" as const,
				text: `Error: ${context} failed - ${response.status} ${response.statusText}`,
			},
		],
		isError: true,
	};
}

/**
 * Check if bearer token is available and return appropriate error message if not
 */
export function checkBearerToken(bearerToken: string | undefined) {
	if (!bearerToken) {
		return {
			content: [
				{
					type: "text" as const,
					text: "Please set your API key in the Jina AI MCP settings. You can get a free API key by visiting https://jina.ai and signing up for an account.",
				},
			],
			isError: true,
		};
	}
	return null; // No error, token is available
}