LibreChat/packages/api/src/mcp/parsers.ts
Danny Avila 31bb9d255f
📦 chore: Bump MCP SDK: Fix Types and MCP OAuth due to Update (#10811)
* chore: Bump @modelcontextprotocol/sdk to version 1.24.3

* refactor: Update resource handling in MCP parsers and types

- Simplified resource text checks in `parseAsString` and `formatToolContent` functions to ensure proper existence checks.
- Removed unnecessary resource name and description handling to streamline output.
- Updated type definitions in `index.ts` to align with the new structure from `@modelcontextprotocol/sdk`, enhancing type safety and clarity.
- Added `logo_uri` and `tos_uri` properties to `MCPOAuthHandler` for improved OAuth metadata support.

* refactor: Update custom endpoint configurations and type definitions

- Removed unused type imports and streamlined the custom parameters handling in `loadCustomEndpointsConfig`.
- Adjusted the `TCustomEndpointsConfig` type to utilize `TConfig` instead of `TEndpoint`, enhancing type accuracy.
- Made the endpoint schema optional in the configuration to improve flexibility.

* fix: Implement token cleanup and error handling for invalid OAuth tokens

- Added `cleanupInvalidTokens` method to remove invalid OAuth tokens from storage when detected.
- Introduced `isInvalidTokenError` method to identify errors indicating revoked or expired tokens.
- Integrated token cleanup logic into the connection attempt process to ensure fresh OAuth flow on invalid token detection.

* feat: Add revoke OAuth functionality in Server Initialization

- Introduced a new button to revoke OAuth for servers, enhancing user control over OAuth permissions.
- Updated the `useMCPServerManager` hook to include a standalone `revokeOAuthForServer` function for managing OAuth revocation.
- Adjusted the UI to conditionally render the revoke button based on server requirements.

* fix: error handling for authentication in MCPConnection

- Updated the error handling logic in MCPConnection to better identify various authentication error indicators, including 401 status, invalid tokens, and unauthorized messages.
- Removed the deprecated cleanupInvalidTokens method and integrated its logic into the connection attempt process for improved clarity and efficiency.
- Adjusted the MCPConnectionFactory to streamline the connection attempt process and handle OAuth errors more effectively.

* refactor: Update button rendering in ServerInitializationSection

- Removed the existing button for server initialization and replaced it with a new button implementation, maintaining the same functionality.
- Ensured consistent rendering of the button within the component's layout.

* chore: update resource type usage in parsers.test.ts
2025-12-04 19:52:32 -05:00

180 lines
5.3 KiB
TypeScript

import { Tools } from 'librechat-data-provider';
import type { UIResource } from 'librechat-data-provider';
import type * as t from './types';
const RECOGNIZED_PROVIDERS = new Set([
'google',
'anthropic',
'openai',
'azureopenai',
'openrouter',
'xai',
'deepseek',
'ollama',
'bedrock',
]);
const CONTENT_ARRAY_PROVIDERS = new Set(['google', 'anthropic', 'azureopenai', 'openai']);
const imageFormatters: Record<string, undefined | t.ImageFormatter> = {
// google: (item) => ({
// type: 'image',
// inlineData: {
// mimeType: item.mimeType,
// data: item.data,
// },
// }),
// anthropic: (item) => ({
// type: 'image',
// source: {
// type: 'base64',
// media_type: item.mimeType,
// data: item.data,
// },
// }),
default: (item) => ({
type: 'image_url',
image_url: {
url: item.data.startsWith('http') ? item.data : `data:${item.mimeType};base64,${item.data}`,
},
}),
};
function isImageContent(item: t.ToolContentPart): item is t.ImageContent {
return item.type === 'image';
}
function parseAsString(result: t.MCPToolCallResponse): string {
const content = result?.content ?? [];
if (!content.length) {
return '(No response)';
}
const text = content
.map((item) => {
if (item.type === 'text') {
return item.text;
}
if (item.type === 'resource') {
const resourceText = [];
if ('text' in item.resource && item.resource.text != null && item.resource.text) {
resourceText.push(item.resource.text);
}
if (item.resource.uri) {
resourceText.push(`Resource URI: ${item.resource.uri}`);
}
if (item.resource.mimeType != null && item.resource.mimeType) {
resourceText.push(`Type: ${item.resource.mimeType}`);
}
return resourceText.join('\n');
}
return JSON.stringify(item, null, 2);
})
.filter(Boolean)
.join('\n\n');
return text;
}
/**
* Converts MCPToolCallResponse content into recognized content block types
* First element: string or formatted content (excluding image_url)
* Second element: Recognized types - "image", "image_url", "text", "json"
*
* @param result - The MCPToolCallResponse object
* @param provider - The provider name (google, anthropic, openai)
* @returns Tuple of content and image_urls
*/
export function formatToolContent(
result: t.MCPToolCallResponse,
provider: t.Provider,
): t.FormattedContentResult {
if (!RECOGNIZED_PROVIDERS.has(provider)) {
return [parseAsString(result), undefined];
}
const content = result?.content ?? [];
if (!content.length) {
return [[{ type: 'text', text: '(No response)' }], undefined];
}
const formattedContent: t.FormattedContent[] = [];
const imageUrls: t.FormattedContent[] = [];
let currentTextBlock = '';
const uiResources: UIResource[] = [];
type ContentHandler = undefined | ((item: t.ToolContentPart) => void);
const contentHandlers: {
text: (item: Extract<t.ToolContentPart, { type: 'text' }>) => void;
image: (item: t.ToolContentPart) => void;
resource: (item: Extract<t.ToolContentPart, { type: 'resource' }>) => void;
} = {
text: (item) => {
currentTextBlock += (currentTextBlock ? '\n\n' : '') + item.text;
},
image: (item) => {
if (!isImageContent(item)) {
return;
}
if (CONTENT_ARRAY_PROVIDERS.has(provider) && currentTextBlock) {
formattedContent.push({ type: 'text', text: currentTextBlock });
currentTextBlock = '';
}
const formatter = imageFormatters.default as t.ImageFormatter;
const formattedImage = formatter(item);
if (formattedImage.type === 'image_url') {
imageUrls.push(formattedImage);
} else {
formattedContent.push(formattedImage);
}
},
resource: (item) => {
if (item.resource.uri.startsWith('ui://')) {
uiResources.push(item.resource as UIResource);
}
const resourceText = [];
if ('text' in item.resource && item.resource.text != null && item.resource.text) {
resourceText.push(`Resource Text: ${item.resource.text}`);
}
if (item.resource.uri.length) {
resourceText.push(`Resource URI: ${item.resource.uri}`);
}
if (item.resource.mimeType != null && item.resource.mimeType) {
resourceText.push(`Resource MIME Type: ${item.resource.mimeType}`);
}
currentTextBlock += (currentTextBlock ? '\n\n' : '') + resourceText.join('\n');
},
};
for (const item of content) {
const handler = contentHandlers[item.type as keyof typeof contentHandlers] as ContentHandler;
if (handler) {
handler(item as never);
} else {
const stringified = JSON.stringify(item, null, 2);
currentTextBlock += (currentTextBlock ? '\n\n' : '') + stringified;
}
}
if (CONTENT_ARRAY_PROVIDERS.has(provider) && currentTextBlock) {
formattedContent.push({ type: 'text', text: currentTextBlock });
}
let artifacts: t.Artifacts = undefined;
if (imageUrls.length || uiResources.length) {
artifacts = {
...(imageUrls.length && { content: imageUrls }),
...(uiResources.length && { [Tools.ui_resources]: { data: uiResources } }),
};
}
if (CONTENT_ARRAY_PROVIDERS.has(provider)) {
return [formattedContent, artifacts];
}
return [currentTextBlock, artifacts];
}