Skip to content

feat: Allow any value type in get_prompt arguments #1058

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

arjunaskykok
Copy link

The type hints for the arguments parameter in ClientSession.get_prompt and the corresponding GetPromptRequestParams were previously restricted to dict[str, str].

This was overly restrictive and prevented passing arguments with non-string values, such as numbers or booleans. This commit changes the type to dict[str, Any], allowing for more flexible prompt argument structures.

A corresponding test case has been added to verify that calling get_prompt with an integer argument now works as expected.

Fixes #749

Motivation and Context

Imagine this MCP server code:

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("My MCP Server", host="127.0.0.1", port=9000)

@mcp.prompt()
def get_employee_profile(employee_id: int) -> str:
    return f"Using SQL, get the employee profile from the id: {employee_id}"

if __name__ == "__main__":
    mcp.run(transport="streamable-http")

And this client code:

import os
import json
import asyncio
from mcp import ClientSession, StdioServerParameters, types
from mcp.client.stdio import stdio_client
from mcp.client.streamable_http import streamablehttp_client

MCP_SERVER_URL = "http://127.0.0.1:9000/mcp/"

async def run():
    async with streamablehttp_client(MCP_SERVER_URL) as (read, write, _):
        async with ClientSession(
            read, write
        ) as session:
            await session.initialize()

            # Read prompt
            output = await session.get_prompt("get_employee_profile", {"employee_id": "77"}) # this works
            output = await session.get_prompt("get_employee_profile", {"employee_id": 77}) # this fails but we have declared the argument type to be int in the server code. BUG

            print(output.messages[0].content.text)


if __name__ == "__main__":
    asyncio.run(run())

It's very strange that we have to send a string argument although the argument type in the MCP server is int.

This PR also gives us flexibility to send an argument with a different type, like a list of strings, as referenced in the bug report.

The argument in calling the tool is not restricted to string. Why do we need to restrict it in the prompt case?

How Has This Been Tested?

I have added a unit test.

Breaking Changes

Nope.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

I was not sure where to put the unit test. The test_session.py is not ideal. But I couldn't find a better place.

The type hints for the `arguments` parameter in `ClientSession.get_prompt` and
the corresponding `GetPromptRequestParams` were previously restricted to `dict[str, str]`.

This was overly restrictive and prevented passing arguments with non-string values,
such as numbers or booleans. This commit changes the type to `dict[str, Any]`, allowing
for more flexible prompt argument structures.

A corresponding test case has been added to verify that calling `get_prompt` with an integer
argument now works as expected.

Fixes modelcontextprotocol#749
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.

Support anything other than strings in GetPromptRequestParams
1 participant