Skip to content

Add FileResourceContents for file-based resource results#225

Open
liliankasem wants to merge 3 commits intomainfrom
liliankasem/file-resource-contents
Open

Add FileResourceContents for file-based resource results#225
liliankasem wants to merge 3 commits intomainfrom
liliankasem/file-resource-contents

Conversation

@liliankasem
Copy link
Copy Markdown
Member

Issue describing the changes in this PR

resolves #158

Pull request checklist

  • My changes do not require documentation changes
    • Otherwise: Documentation issue linked to PR
  • My changes should not be added to the release notes for the next release
    • Otherwise: I've added my notes to release_notes.md
  • I have added all required tests (Unit tests, E2E tests)

Additional information

Introduces FileResourceContents, a new return type for resource functions that abstracts away file I/O. Instead of manually reading files with Path.Combine(AppContext.BaseDirectory, ...) + File.ReadAllText/ReadAllBytes, users return a FileResourceContents with just a Path — the framework handles the rest.

[Function(nameof(GetImage))]
   public FileResourceContents GetImage(
       [McpResourceTrigger("file://logo.png", "logo", MimeType = "image/png")]
       ResourceInvocationContext context)
   {
       return new FileResourceContents { Path = "assets/logo.png" };
   }

@liliankasem liliankasem force-pushed the liliankasem/file-resource-contents branch from 2f661d5 to 9fc443b Compare April 1, 2026 16:26
@liliankasem liliankasem closed this Apr 1, 2026
@liliankasem liliankasem reopened this Apr 2, 2026
@liliankasem liliankasem changed the title Add FileResourceContents for file-based resource results [WIP] Add FileResourceContents for file-based resource results Apr 2, 2026
@liliankasem liliankasem force-pushed the liliankasem/file-resource-contents branch from 08c354c to 8dc82e3 Compare April 7, 2026 16:12
liliankasem and others added 2 commits April 7, 2026 09:13
Introduce FileResourceContents type that allows resource functions to
return file paths instead of manually reading files. The framework
handles file I/O and text/binary detection automatically.

New types:
- FileResourceContents: public model with Path (required) and MimeType (optional)
- FunctionsMcpResourceResultMiddleware: reads files and converts to
  TextResourceContents or BlobResourceContents based on MIME type
- MimeTypeHelper: determines text vs binary encoding from MIME type
- McpResourceResult: SDK-side model matching host's expected shape

Features:
- Relative paths resolved against AppContext.BaseDirectory automatically
- MIME-based encoding: text/* and application/json,xml,js → UTF-8 text;
  structured suffixes (+json, +xml) → text; everything else → base64 blob
- Falls back to attribute MimeType from binding context when not set
- Null/empty MIME type defaults to text

Closes #158

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@liliankasem liliankasem force-pushed the liliankasem/file-resource-contents branch from 8dc82e3 to 9b8d7ff Compare April 7, 2026 16:13
@liliankasem liliankasem changed the title [WIP] Add FileResourceContents for file-based resource results Add FileResourceContents for file-based resource results Apr 7, 2026
@liliankasem liliankasem marked this pull request as ready for review April 7, 2026 16:13
…HelperTests

- Seal FileResourceContents to match codebase conventions
- Revert WorkerMcpJsonContext rename back to McpJsonContext
- Extract MimeTypeHelper tests into dedicated test class
- Add test for lowercase mimeType binding data key

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement File resource abstraction

1 participant