-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
drop media content from MCP message attributes #17864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
naaa760
wants to merge
11
commits into
getsentry:develop
Choose a base branch
from
naaa760:msgs-drop-media
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+269
−27
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7bd48c1
new file with media detection and filtering logic
naaa760 526687c
updated to filter media from tool and prompt results
naaa760 4807888
aded documentation about media dropping behavior
naaa760 cccf345
update
naaa760 dcbc924
update
naaa760 7932dba
update
naaa760 f6f5dac
update
naaa760 f965f45
update
naaa760 5bddc4c
reverted
naaa760 28bfcba
update
naaa760 d828f59
Merge branch 'develop' into msgs-drop-media
naaa760 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
packages/core/src/integrations/mcp-server/mediaFiltering.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { getClient } from '../../currentScopes'; | ||
import { isValidContentItem } from './validation'; | ||
|
||
const MEDIA_MIME_TYPES = new Set([ | ||
'image/jpeg', | ||
'image/jpg', | ||
'image/png', | ||
'image/gif', | ||
'image/webp', | ||
'image/svg+xml', | ||
'image/bmp', | ||
'image/tiff', | ||
'video/mp4', | ||
'video/avi', | ||
'video/mov', | ||
'video/wmv', | ||
'video/flv', | ||
'video/webm', | ||
'video/mkv', | ||
'audio/mp3', | ||
'audio/wav', | ||
'audio/ogg', | ||
'audio/mpeg', | ||
'audio/aac', | ||
'audio/flac', | ||
'application/pdf', | ||
'application/zip', | ||
'application/x-zip-compressed', | ||
]); | ||
|
||
function isMediaContent(item: unknown): boolean { | ||
if (!isValidContentItem(item)) { | ||
return false; | ||
} | ||
|
||
if (typeof item.type === 'string' && item.type === 'image') { | ||
return true; | ||
} | ||
|
||
if (typeof item.mimeType === 'string' && MEDIA_MIME_TYPES.has(item.mimeType.toLowerCase())) { | ||
return true; | ||
} | ||
|
||
if (typeof item.data === 'string' && item.data.length > 1000) { | ||
const dataStart = item.data.substring(0, 50).toLowerCase(); | ||
if (dataStart.includes('data:image/') || dataStart.includes('/9j/') || dataStart.includes('iVBORw0KGgo')) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function recordDroppedMedia(reason: string, count: number = 1): void { | ||
const client = getClient(); | ||
if (client) { | ||
client.recordDroppedEvent(reason as any, 'attachment', count); | ||
} | ||
} | ||
|
||
export function filterMediaFromContentItem(item: unknown): unknown | null { | ||
if (!isValidContentItem(item)) { | ||
return item; | ||
} | ||
|
||
if (isMediaContent(item)) { | ||
recordDroppedMedia('media_content_dropped'); | ||
return null; | ||
} | ||
|
||
if (Array.isArray(item.content)) { | ||
const filteredContent = item.content | ||
.map(contentItem => { | ||
if (isMediaContent(contentItem)) { | ||
recordDroppedMedia('media_content_dropped'); | ||
return null; | ||
} | ||
return contentItem; | ||
}) | ||
.filter(contentItem => contentItem !== null); | ||
|
||
if (filteredContent.length === 0) { | ||
return null; | ||
} | ||
|
||
return { | ||
...item, | ||
content: filteredContent, | ||
}; | ||
} | ||
|
||
if (isValidContentItem(item.content) && isMediaContent(item.content)) { | ||
recordDroppedMedia('media_content_dropped'); | ||
return null; | ||
} | ||
|
||
return item; | ||
} | ||
|
||
export function filterMediaFromContentArray(content: unknown[]): unknown[] { | ||
return content | ||
.map(item => filterMediaFromContentItem(item)) | ||
.filter(item => item !== null); | ||
} | ||
|
||
export function filterMediaFromAttributes(attributes: Record<string, unknown>): Record<string, unknown> { | ||
const filteredAttributes = { ...attributes }; | ||
|
||
for (const [key, value] of Object.entries(filteredAttributes)) { | ||
if (Array.isArray(value)) { | ||
if (value.length > 0 && value.some(item => isValidContentItem(item))) { | ||
const filtered = filterMediaFromContentArray(value); | ||
if (filtered.length === 0) { | ||
delete filteredAttributes[key]; | ||
} else { | ||
filteredAttributes[key] = filtered; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return filteredAttributes; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { getClient } from '../../currentScopes'; | ||
|
||
const MEDIA_MIME_TYPES = new Set([ | ||
'image/jpeg', | ||
'image/jpg', | ||
'image/png', | ||
'image/gif', | ||
'image/webp', | ||
'image/svg+xml', | ||
'image/bmp', | ||
'image/tiff', | ||
'video/mp4', | ||
'video/avi', | ||
'video/mov', | ||
'video/wmv', | ||
'video/flv', | ||
'video/webm', | ||
'video/mkv', | ||
'audio/mp3', | ||
'audio/wav', | ||
'audio/ogg', | ||
'audio/mpeg', | ||
'audio/aac', | ||
'audio/flac', | ||
'application/pdf', | ||
'application/zip', | ||
'application/x-zip-compressed', | ||
]); | ||
|
||
function isMediaContent(item: unknown): boolean { | ||
if (typeof item !== 'object' || item === null) { | ||
return false; | ||
} | ||
|
||
const obj = item as Record<string, unknown>; | ||
|
||
if (typeof obj.type === 'string' && (obj.type === 'image' || obj.type === 'image_url')) { | ||
return true; | ||
} | ||
|
||
if (typeof obj.mime_type === 'string' && MEDIA_MIME_TYPES.has(obj.mime_type.toLowerCase())) { | ||
return true; | ||
} | ||
|
||
if (typeof obj.mimeType === 'string' && MEDIA_MIME_TYPES.has(obj.mimeType.toLowerCase())) { | ||
return true; | ||
} | ||
|
||
if (typeof obj.data === 'string' && obj.data.length > 1000) { | ||
const dataStart = obj.data.substring(0, 50).toLowerCase(); | ||
if (dataStart.includes('data:image/') || dataStart.includes('/9j/') || dataStart.includes('ivborw0kggo')) { | ||
return true; | ||
} | ||
} | ||
|
||
if (typeof obj.source === 'object' && obj.source !== null) { | ||
const source = obj.source as Record<string, unknown>; | ||
if (typeof source.type === 'string' && source.type === 'base64' && typeof source.data === 'string') { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function recordDroppedMedia(count: number = 1): void { | ||
const client = getClient(); | ||
if (client) { | ||
client.recordDroppedEvent('media_content_dropped' as any, 'attachment', count); | ||
} | ||
} | ||
|
||
export function filterMediaFromMessages(messages: unknown): unknown { | ||
if (!Array.isArray(messages)) { | ||
return messages; | ||
} | ||
|
||
let droppedCount = 0; | ||
|
||
const filtered = messages.map(message => { | ||
if (typeof message !== 'object' || message === null) { | ||
return message; | ||
} | ||
|
||
const msg = message as Record<string, unknown>; | ||
|
||
if (Array.isArray(msg.content)) { | ||
const filteredContent = msg.content.filter(item => { | ||
if (isMediaContent(item)) { | ||
droppedCount++; | ||
return false; | ||
} | ||
return true; | ||
}); | ||
|
||
if (filteredContent.length === 0) { | ||
return { ...msg, content: '' }; | ||
} | ||
|
||
return { ...msg, content: filteredContent }; | ||
} | ||
|
||
if (isMediaContent(msg.content)) { | ||
droppedCount++; | ||
return { ...msg, content: '' }; | ||
} | ||
|
||
return message; | ||
}); | ||
|
||
if (droppedCount > 0) { | ||
recordDroppedMedia(droppedCount); | ||
} | ||
|
||
return filtered; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Undefined Variable Causes Reference Error
A
ReferenceError
occurs becausefilteredMessages.length
is referenced butfilteredMessages
is not defined in this scope. It looks likemessages.length
was the intended variable.