Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
Implementation of SEP 973 - Additional metadata + icons support#1357
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6ad4bb3
Implementation of SEP-973
pja-ant b999b4e
Resize icon to 64x64 and update demo sizes
pja-ant 16ba07a
Add public properties for website_url and icons, add tests
pja-ant 883d2b4
Fix double https:// typo in icons demo website URL
pja-ant d028efb
Add icon documentation to README
pja-ant 7bd4334
Export Icon from FastMCP module for simpler imports
pja-ant 22a9067
Add missing newline at end of test file
pja-ant 10515fb
Fix pre-commit formatting issues
pja-ant 32624d9
icon path parsing suggestion
pja-ant 8353be8
fix arg ordering
pja-ant 0972674
Pre-commit
pja-ant 13b1340
Merge branch 'main' into feature/icon-support
pja-ant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| """ | ||
| FastMCP Icons Demo Server | ||
| Demonstrates using icons with tools, resources, prompts, and implementation. | ||
| """ | ||
| import base64 | ||
| from pathlib import Path | ||
| from mcp.server.fastmcp import FastMCP, Icon | ||
| # Load the icon file and convert to data URI | ||
| icon_path = Path(__file__).parent / "mcp.png" | ||
| icon_data = base64.standard_b64encode(icon_path.read_bytes()).decode() | ||
| icon_data_uri = f"data:image/png;base64,{icon_data}" | ||
| icon_data = Icon(src=icon_data_uri, mimeType="image/png", sizes="64x64") | ||
| # Create server with icons in implementation | ||
| mcp = FastMCP("Icons Demo Server", website_url="https://github.com/modelcontextprotocol/python-sdk", icons=[icon_data]) | ||
| @mcp.tool(icons=[icon_data]) | ||
| def demo_tool(message: str) -> str: | ||
| """A demo tool with an icon.""" | ||
| return message | ||
| @mcp.resource("demo://readme", icons=[icon_data]) | ||
| def readme_resource() -> str: | ||
| """A demo resource with an icon""" | ||
| return "This resource has an icon" | ||
| @mcp.prompt("prompt_with_icon", icons=[icon_data]) | ||
| def prompt_with_icon(text: str) -> str: | ||
| """A demo prompt with an icon""" | ||
| return text | ||
| @mcp.tool( | ||
| icons=[ | ||
| Icon(src=icon_data_uri, mimeType="image/png", sizes="16x16"), | ||
| Icon(src=icon_data_uri, mimeType="image/png", sizes="32x32"), | ||
| Icon(src=icon_data_uri, mimeType="image/png", sizes="64x64"), | ||
| ] | ||
| ) | ||
| def multi_icon_tool(action: str) -> str: | ||
| """A tool demonstrating multiple icons.""" | ||
| return "multi_icon_tool" | ||
| if __name__ == "__main__": | ||
| # Run the server | ||
| mcp.run() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
|---|---|---|
| @@ -43,7 +43,7 @@ | ||
| from mcp.server.streamable_http_manager import StreamableHTTPSessionManager | ||
| from mcp.server.transport_security import TransportSecuritySettings | ||
| from mcp.shared.context import LifespanContextT, RequestContext, RequestT | ||
| from mcp.types import AnyFunction, ContentBlock, GetPromptResult, ToolAnnotations | ||
| from mcp.types import AnyFunction, ContentBlock, GetPromptResult, Icon, ToolAnnotations | ||
| from mcp.types import Prompt as MCPPrompt | ||
| from mcp.types import PromptArgument as MCPPromptArgument | ||
| from mcp.types import Resource as MCPResource | ||
| @@ -120,10 +120,12 @@ async def wrap(_: MCPServer[LifespanResultT, Request]) -> AsyncIterator[Lifespan | ||
| class FastMCP(Generic[LifespanResultT]): | ||
| def __init__( | ||
| def __init__( # noqa: PLR0913 | ||
pja-ant marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| self, | ||
| name: str | None = None, | ||
| instructions: str | None = None, | ||
| website_url: str | None = None, | ||
| icons: list[Icon] | None = None, | ||
| auth_server_provider: OAuthAuthorizationServerProvider[Any, Any, Any] | None = None, | ||
| token_verifier: TokenVerifier | None = None, | ||
| event_store: EventStore | None = None, | ||
| @@ -170,6 +172,8 @@ def __init__( | ||
| self._mcp_server = MCPServer( | ||
| name=name or "FastMCP", | ||
| instructions=instructions, | ||
| website_url=website_url, | ||
| icons=icons, | ||
| # TODO(Marcelo): It seems there's a type mismatch between the lifespan type from an FastMCP and Server. | ||
| # We need to create a Lifespan type that is a generic on the server type, like Starlette does. | ||
| lifespan=(lifespan_wrapper(self, self.settings.lifespan) if self.settings.lifespan else default_lifespan), # type: ignore | ||
| @@ -211,6 +215,14 @@ def name(self) -> str: | ||
| def instructions(self) -> str | None: | ||
| return self._mcp_server.instructions | ||
| @property | ||
| def website_url(self) -> str | None: | ||
| return self._mcp_server.website_url | ||
| @property | ||
| def icons(self) -> list[Icon] | None: | ||
| return self._mcp_server.icons | ||
| @property | ||
| def session_manager(self) -> StreamableHTTPSessionManager: | ||
| """Get the StreamableHTTP session manager. | ||
| @@ -277,6 +289,7 @@ async def list_tools(self) -> list[MCPTool]: | ||
| inputSchema=info.parameters, | ||
| outputSchema=info.output_schema, | ||
| annotations=info.annotations, | ||
| icons=info.icons, | ||
| ) | ||
| for info in tools | ||
| ] | ||
| @@ -308,6 +321,7 @@ async def list_resources(self) -> list[MCPResource]: | ||
| title=resource.title, | ||
| description=resource.description, | ||
| mimeType=resource.mime_type, | ||
| icons=resource.icons, | ||
| ) | ||
| for resource in resources | ||
| ] | ||
| @@ -347,6 +361,7 @@ def add_tool( | ||
| title: str | None = None, | ||
| description: str | None = None, | ||
| annotations: ToolAnnotations | None = None, | ||
| icons: list[Icon] | None = None, | ||
| structured_output: bool | None = None, | ||
| ) -> None: | ||
| """Add a tool to the server. | ||
| @@ -371,6 +386,7 @@ def add_tool( | ||
| title=title, | ||
| description=description, | ||
| annotations=annotations, | ||
| icons=icons, | ||
| structured_output=structured_output, | ||
| ) | ||
| @@ -380,6 +396,7 @@ def tool( | ||
| title: str | None = None, | ||
| description: str | None = None, | ||
| annotations: ToolAnnotations | None = None, | ||
| icons: list[Icon] | None = None, | ||
| structured_output: bool | None = None, | ||
| ) -> Callable[[AnyFunction], AnyFunction]: | ||
| """Decorator to register a tool. | ||
| @@ -426,6 +443,7 @@ def decorator(fn: AnyFunction) -> AnyFunction: | ||
| title=title, | ||
| description=description, | ||
| annotations=annotations, | ||
| icons=icons, | ||
| structured_output=structured_output, | ||
| ) | ||
| return fn | ||
| @@ -466,6 +484,7 @@ def resource( | ||
| title: str | None = None, | ||
| description: str | None = None, | ||
| mime_type: str | None = None, | ||
| icons: list[Icon] | None = None, | ||
| ) -> Callable[[AnyFunction], AnyFunction]: | ||
| """Decorator to register a function as a resource. | ||
| @@ -540,6 +559,7 @@ def decorator(fn: AnyFunction) -> AnyFunction: | ||
| title=title, | ||
| description=description, | ||
| mime_type=mime_type, | ||
| # Note: Resource templates don't support icons | ||
| ) | ||
| else: | ||
| # Register as regular resource | ||
| @@ -550,6 +570,7 @@ def decorator(fn: AnyFunction) -> AnyFunction: | ||
| title=title, | ||
| description=description, | ||
| mime_type=mime_type, | ||
| icons=icons, | ||
| ) | ||
| self.add_resource(resource) | ||
| return fn | ||
| @@ -565,7 +586,11 @@ def add_prompt(self, prompt: Prompt) -> None: | ||
| self._prompt_manager.add_prompt(prompt) | ||
| def prompt( | ||
| self, name: str | None = None, title: str | None = None, description: str | None = None | ||
| self, | ||
| name: str | None = None, | ||
| title: str | None = None, | ||
| description: str | None = None, | ||
| icons: list[Icon] | None = None, | ||
| ) -> Callable[[AnyFunction], AnyFunction]: | ||
| """Decorator to register a prompt. | ||
| @@ -609,7 +634,7 @@ async def analyze_file(path: str) -> list[Message]: | ||
| ) | ||
| def decorator(func: AnyFunction) -> AnyFunction: | ||
| prompt = Prompt.from_function(func, name=name, title=title, description=description) | ||
| prompt = Prompt.from_function(func, name=name, title=title, description=description, icons=icons) | ||
| self.add_prompt(prompt) | ||
| return func | ||
| @@ -980,6 +1005,7 @@ async def list_prompts(self) -> list[MCPPrompt]: | ||
| ) | ||
| for arg in (prompt.arguments or []) | ||
| ], | ||
| icons=prompt.icons, | ||
| ) | ||
| for prompt in prompts | ||
| ] | ||
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.