-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add simple resource example #49
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.11 |
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,36 @@ | ||
# MCP Simple Resource | ||
|
||
A simple MCP server that exposes sample text files as resources. | ||
|
||
## Usage | ||
|
||
Start the server using either stdio (default) or SSE transport: | ||
|
||
```bash | ||
# Using stdio transport (default) | ||
mcp-simple-resource | ||
|
||
# Using SSE transport on custom port | ||
mcp-simple-resource --transport sse --port 8000 | ||
``` | ||
|
||
The server exposes some basic text file resources that can be read by clients. | ||
|
||
## Example | ||
|
||
Using the MCP client, you can read the resources like this: | ||
|
||
```python | ||
from mcp.client import ClientSession | ||
|
||
async with ClientSession() as session: | ||
await session.initialize() | ||
|
||
# List available resources | ||
resources = await session.list_resources() | ||
print(resources) | ||
|
||
# Read a specific resource | ||
resource = await session.read_resource(resources[0].uri) | ||
print(resource) | ||
``` |
1 change: 1 addition & 0 deletions
1
examples/servers/simple-resource/mcp_simple_resource/__init__.py
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 @@ | ||
|
5 changes: 5 additions & 0 deletions
5
examples/servers/simple-resource/mcp_simple_resource/__main__.py
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,5 @@ | ||
import sys | ||
|
||
from server import main | ||
|
||
sys.exit(main()) |
89 changes: 89 additions & 0 deletions
89
examples/servers/simple-resource/mcp_simple_resource/server.py
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,89 @@ | ||
import anyio | ||
import click | ||
import mcp.types as types | ||
from mcp.server import AnyUrl, Server | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
|
||
@cli.command() | ||
@click.option("--port", default=8000, help="Port to listen on for SSE") | ||
@click.option( | ||
"--transport", | ||
type=click.Choice(["stdio", "sse"]), | ||
default="stdio", | ||
help="Transport type", | ||
) | ||
def main(port: int, transport: str) -> int: | ||
return anyio.run(_amain, port, transport) | ||
|
||
|
||
SAMPLE_RESOURCES = { | ||
"greeting": "Hello! This is a sample text resource.", | ||
"help": "This server provides a few sample text resources for testing.", | ||
"about": "This is the simple-resource MCP server implementation.", | ||
} | ||
|
||
|
||
async def _amain(port: int, transport: str) -> int: | ||
app = Server("mcp-simple-resource") | ||
|
||
@app.list_resources() | ||
async def list_resources() -> list[types.Resource]: | ||
return [ | ||
types.Resource( | ||
uri=AnyUrl(f"file:///{name}.txt"), | ||
name=name, | ||
description=f"A sample text resource named {name}", | ||
mimeType="text/plain", | ||
) | ||
for name in SAMPLE_RESOURCES.keys() | ||
] | ||
|
||
@app.read_resource() | ||
async def read_resource(uri: AnyUrl) -> str | bytes: | ||
assert uri.path is not None | ||
name = uri.path.replace(".txt", "").lstrip("/") | ||
|
||
if name not in SAMPLE_RESOURCES: | ||
raise ValueError(f"Unknown resource: {uri}") | ||
|
||
return SAMPLE_RESOURCES[name] | ||
|
||
if transport == "sse": | ||
from mcp.server.sse import SseServerTransport | ||
from starlette.applications import Starlette | ||
from starlette.routing import Route | ||
|
||
sse = SseServerTransport("/messages") | ||
|
||
async def handle_sse(scope, receive, send): | ||
async with sse.connect_sse(scope, receive, send) as streams: | ||
await app.run( | ||
streams[0], streams[1], app.create_initialization_options() | ||
) | ||
|
||
async def handle_messages(scope, receive, send): | ||
await sse.handle_post_message(scope, receive, send) | ||
|
||
starlette_app = Starlette( | ||
debug=True, | ||
routes=[ | ||
Route("/sse", endpoint=handle_sse), | ||
Route("/messages", endpoint=handle_messages, methods=["POST"]), | ||
], | ||
) | ||
|
||
import uvicorn | ||
|
||
uvicorn.run(starlette_app, host="0.0.0.0", port=port) | ||
else: | ||
from mcp.server.stdio import stdio_server | ||
|
||
async with stdio_server() as streams: | ||
await app.run(streams[0], streams[1], app.create_initialization_options()) | ||
|
||
return 0 |
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,47 @@ | ||
[project] | ||
name = "mcp-simple-resource" | ||
version = "0.1.0" | ||
description = "A simple MCP server exposing sample text resources" | ||
readme = "README.md" | ||
requires-python = ">=3.10" | ||
authors = [{ name = "Anthropic, PBC." }] | ||
maintainers = [ | ||
{ name = "David Soria Parra", email = "[email protected]" }, | ||
{ name = "Justin Spahr-Summers", email = "[email protected]" }, | ||
] | ||
keywords = ["mcp", "llm", "automation", "web", "fetch"] | ||
license = { text = "MIT" } | ||
classifiers = [ | ||
"Development Status :: 4 - Beta", | ||
"Intended Audience :: Developers", | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3.10", | ||
] | ||
dependencies = ["anyio>=4.6.2.post1", "click>=8.1.7", "httpx>=0.27.2", "mcp"] | ||
|
||
[project.scripts] | ||
mcp-simple-resource = "mcp_simple_resource.server:main" | ||
|
||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[tool.hatch.build.targets.wheel] | ||
packages = ["mcp_simple_resource"] | ||
|
||
[tool.pyright] | ||
include = ["mcp_simple_resource"] | ||
venvPath = "." | ||
venv = ".venv" | ||
|
||
[tool.ruff.lint] | ||
select = ["E", "F", "I"] | ||
ignore = [] | ||
|
||
[tool.ruff] | ||
line-length = 88 | ||
target-version = "py310" | ||
|
||
[tool.uv] | ||
dev-dependencies = ["pyright>=1.1.378", "pytest>=8.3.3", "ruff>=0.6.9"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bonus points: actually put these on disk alongside the server code 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or serve the code itself :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll follow up potentially, for now i'd just love to get it in and focus on all the other parts of the code that needs work on.