-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Feat/mcp download support #843
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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,90 @@ | ||
| import { createAdminClient } from "@api/services/supabase"; | ||
| import { download, signedUrl } from "@midday/supabase/storage"; | ||
|
|
||
| export type TextContent = { type: "text"; text: string }; | ||
| export type ResourceContent = { | ||
| type: "resource"; | ||
| resource: { uri: string; mimeType: string; blob: string }; | ||
| }; | ||
| export type McpContent = TextContent | ResourceContent; | ||
|
|
||
| const MIME_TYPES: Record<string, string> = { | ||
| pdf: "application/pdf", | ||
| jpg: "image/jpeg", | ||
| jpeg: "image/jpeg", | ||
| png: "image/png", | ||
| gif: "image/gif", | ||
| webp: "image/webp", | ||
| csv: "text/csv", | ||
| xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | ||
| }; | ||
|
|
||
| const SIGNED_URL_EXPIRY = 60 * 60; // 1 hour | ||
|
|
||
| export function getMimeType(filename: string): string { | ||
| const ext = filename.split(".").pop()?.toLowerCase(); | ||
| return MIME_TYPES[ext || ""] || "application/octet-stream"; | ||
| } | ||
|
|
||
| /** | ||
| * Generate a presigned Supabase storage URL for a vault file. | ||
| * Expires after 1 hour. | ||
| */ | ||
| export async function getVaultSignedUrl( | ||
| storagePath: string, | ||
| ): Promise<string | null> { | ||
| const supabase = await createAdminClient(); | ||
| const { data, error } = await signedUrl(supabase, { | ||
| bucket: "vault", | ||
| path: storagePath, | ||
| expireIn: SIGNED_URL_EXPIRY, | ||
| }); | ||
|
|
||
| if (error || !data?.signedUrl) return null; | ||
| return data.signedUrl; | ||
| } | ||
|
|
||
| /** | ||
| * Download a file from Supabase vault storage and return it as | ||
| * a base64-encoded MCP EmbeddedResource content item. | ||
| */ | ||
| export async function downloadVaultFile( | ||
| storagePath: string, | ||
| uri: string, | ||
| mimeType: string, | ||
| ): Promise<ResourceContent | null> { | ||
| const supabase = await createAdminClient(); | ||
| const { data } = await download(supabase, { | ||
| bucket: "vault", | ||
| path: storagePath, | ||
| }); | ||
|
|
||
| if (!data) return null; | ||
|
|
||
| const buffer = await data.arrayBuffer(); | ||
| const blob = Buffer.from(buffer).toString("base64"); | ||
|
|
||
| return { | ||
| type: "resource", | ||
| resource: { uri, mimeType, blob }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Render a stream (e.g. from PDF generation) to a base64-encoded | ||
| * MCP EmbeddedResource content item. | ||
| */ | ||
| export async function streamToResource( | ||
| stream: ReadableStream | NodeJS.ReadableStream, | ||
| uri: string, | ||
| mimeType: string, | ||
| ): Promise<ResourceContent> { | ||
| const response = new Response(stream as any); | ||
| const buffer = await response.arrayBuffer(); | ||
| const blob = Buffer.from(buffer).toString("base64"); | ||
|
|
||
| return { | ||
| type: "resource", | ||
| resource: { uri, mimeType, blob }, | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.