Skip to content

Inject the Request information in MCP tool calls - #11360

Merged
abidlabs merged 16 commits into
mainfrom
mcp-request
Jun 10, 2025
Merged

abidlabs merged 16 commits into
mainfrom
mcp-request

Conversation

@abidlabs

@abidlabs abidlabs commented Jun 9, 2025 •

Copy link
Copy Markdown
Member

Previously, we were not injecting the correct starlette.Request object in a user's function, even if they decorated it with gr.Request (basically the latest user's network request information would be used for all users). Here, we fix that, taking advantage of this PR introduced in the latest version of mcp: modelcontextprotocol/python-sdk#816

You can test this by running an app like this:

import gradio as gr

def echo_headers(x, request: gr.Request):
    return str(dict(request.headers)["fake_username"])

gr.Interface(echo_headers, "textbox", "textbox").launch(mcp_server=True)

and then connect with two different clients, providing different values for the fake_username header:

image

@gradio-pr-bot

gradio-pr-bot commented Jun 9, 2025 •

Copy link
Copy Markdown
Collaborator

🪼 branch checks and previews

• Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/b176db9d6e1bffd36f5b2ecd54b425e4cec3dab8/gradio-5.33.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@b176db9d6e1bffd36f5b2ecd54b425e4cec3dab8#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/b176db9d6e1bffd36f5b2ecd54b425e4cec3dab8/gradio-client-1.15.1.tgz

Use Lite from this PR

<script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/b176db9d6e1bffd36f5b2ecd54b425e4cec3dab8/dist/lite.js""></script>

@gradio-pr-bot

gradio-pr-bot commented Jun 9, 2025 •

Copy link
Copy Markdown
Collaborator

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Inject the Request information in MCP tool calls

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@abidlabs abidlabs added the v: patch A change that requires a patch release label Jun 9, 2025
@abidlabs
abidlabs requested a review from freddyaboulton June 9, 2025 23:07
@abidlabs
abidlabs marked this pull request as ready for review June 9, 2025 23:07
@abidlabs
abidlabs requested a review from aliabid94 June 9, 2025 23:07
@abidlabs abidlabs changed the title Provide correct context requests information in mcp tool call Inject the Request in MCP tool calls Jun 9, 2025
@abidlabs abidlabs changed the title Inject the Request in MCP tool calls Inject the Request information in MCP tool calls Jun 9, 2025
@abidlabs
abidlabs requested review from hysts and yvrjsharma June 9, 2025 23:16

@freddyaboulton freddyaboulton left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly just one question

Comment thread gradio/mcp.py Outdated
arguments: The arguments to pass to the tool.
"""
context_request = self.mcp_server.request_context.request
assert context_request is not None # noqa: S101

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than noqa comment, raise RuntimeError if request_context is None

Comment thread gradio/mcp.py Outdated
route_path="/gradio_api/mcp/messages",
root_path=self.root_path,
)
print("root_url", root_url)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove print

Comment thread gradio/mcp.py Outdated
assert context_request is not None # noqa: S101
root_url = route_utils.get_root_url(
request=context_request,
route_path="/gradio_api/mcp/messages",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If using the http protocol, the path /gradio_api/mcp/http, no? is this going to cause problems?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch I'll fix this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now

@freddyaboulton freddyaboulton left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean @abidlabs !

@abidlabs
abidlabs merged commit 1eb4e2e into main Jun 10, 2025
@abidlabs
abidlabs deleted the mcp-request branch June 10, 2025 00:13
@abidlabs

Copy link
Copy Markdown
Member Author

Thanks so much @freddyaboulton!

Oncorporation pushed a commit to Oncorporation/gradio that referenced this pull request Jun 17, 2025
* changes

* add changeset

* changes

* add changeset

* changes

* simplify

* pin mcp version

* changes

* changes

* undo

* add changeset

* changes

* changes

* format

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
dawoodkhan82 pushed a commit that referenced this pull request Sep 27, 2026
* changes

* add changeset

* changes

* add changeset

* changes

* simplify

* pin mcp version

* changes

* changes

* undo

* add changeset

* changes

* changes

* format

---------

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

Labels

v: patch A change that requires a patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants