Skip to content

fix(mcp): percent-encode filenames in embedded resource URIs - #3706

Open
BichengWang wants to merge 1 commit into
ag2ai:mainfrom
BichengWang:mcp-resource-uri-encoding
Open

BichengWang wants to merge 1 commit into
ag2ai:mainfrom
BichengWang:mcp-resource-uri-encoding

Conversation

@BichengWang

Copy link
Copy Markdown
Contributor

Why are these changes needed?

reply_to_content maps a generated file that is not an image or audio to an EmbeddedResource whose URI is built as resource://{file.name}, with the raw filename (metadata["filename"]). A name with a space, #, ? or / produces a URI that is invalid or that parses into the wrong parts:

  • my report.pdf -> resource://my report.pdf (space in the URI)
  • a#b?.pdf -> urlsplit gives host a, fragment b?.pdf
  • dir/a b.pdf -> host dir, path /a b.pdf

The filename is now percent-encoded (urllib.parse.quote(name, safe="")), so the whole name stays one URI component and decodes back to the original. Names that were already URI-safe are unchanged.

Related issue number

None.

Checks

AI assistance

  • I understand the changes in this PR and can explain them in my own words.
  • I have verified that the PR description accurately reflects the actual diff.
  • If AI assistance was used, I reviewed, tested, and validated the generated code/text before submitting.

Validation run:

  • New parametrized test test_embedded_resource_uri_keeps_filename_intact in test/mcp/test_content_mapping.py: failed before the fix (3 of 4 cases: space, #?, /), passes after.
  • pytest test/mcp: 579 passed.
  • ruff check and ruff format --check on ag2/mcp and test/mcp: clean.
  • Not run: mypy, full repo suite, docs build.

@github-actions github-actions Bot added the area:mcp MCP integration (ag2/mcp) label Nov 21, 2026
@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
ag2/mcp/mappers.py 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:mcp MCP integration (ag2/mcp)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant