chore: import upstream snapshot with attribution
Deploy Docs / deploy-docs (push) Failing after 1s
Conformance Tests / client-conformance (push) Failing after 3s
Conformance Tests / server-conformance (push) Failing after 1s
GitHub Actions Security Analysis / zizmor (push) Failing after 1s
CI / checks (push) Failing after 59m20s
CI / all-green (push) Waiting to run
Deploy Docs / deploy-docs (push) Failing after 1s
Conformance Tests / client-conformance (push) Failing after 3s
Conformance Tests / server-conformance (push) Failing after 1s
GitHub Actions Security Analysis / zizmor (push) Failing after 1s
CI / checks (push) Failing after 59m20s
CI / all-green (push) Waiting to run
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
# MCP Simple Resource
|
||||
|
||||
A simple MCP server that exposes sample text files as resources.
|
||||
|
||||
## Usage
|
||||
|
||||
Start the server using either stdio (default) or Streamable HTTP transport:
|
||||
|
||||
```bash
|
||||
# Using stdio transport (default)
|
||||
uv run mcp-simple-resource
|
||||
|
||||
# Using Streamable HTTP transport on custom port
|
||||
uv run mcp-simple-resource --transport streamable-http --port 8000
|
||||
```
|
||||
|
||||
The server exposes some basic text file resources that can be read by clients.
|
||||
|
||||
## Example
|
||||
|
||||
Using the MCP client, you can retrieve resources like this using the STDIO transport:
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
from pydantic import AnyUrl
|
||||
from mcp.client.session import ClientSession
|
||||
from mcp.client.stdio import StdioServerParameters, stdio_client
|
||||
|
||||
|
||||
async def main():
|
||||
async with stdio_client(
|
||||
StdioServerParameters(command="uv", args=["run", "mcp-simple-resource"])
|
||||
) as (read, write):
|
||||
async with ClientSession(read, write) as session:
|
||||
await session.initialize()
|
||||
|
||||
# List available resources
|
||||
resources = await session.list_resources()
|
||||
print(resources)
|
||||
|
||||
# Get a specific resource
|
||||
resource = await session.read_resource(AnyUrl("file:///greeting.txt"))
|
||||
print(resource)
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
```
|
||||
@@ -0,0 +1,5 @@
|
||||
import sys
|
||||
|
||||
from .server import main
|
||||
|
||||
sys.exit(main()) # type: ignore[call-arg]
|
||||
@@ -0,0 +1,91 @@
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import anyio
|
||||
import click
|
||||
import mcp_types as types
|
||||
from mcp.server import Server, ServerRequestContext
|
||||
|
||||
SAMPLE_RESOURCES = {
|
||||
"greeting": {
|
||||
"content": "Hello! This is a sample text resource.",
|
||||
"title": "Welcome Message",
|
||||
},
|
||||
"help": {
|
||||
"content": "This server provides a few sample text resources for testing.",
|
||||
"title": "Help Documentation",
|
||||
},
|
||||
"about": {
|
||||
"content": "This is the simple-resource MCP server implementation.",
|
||||
"title": "About This Server",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
async def handle_list_resources(
|
||||
ctx: ServerRequestContext, params: types.PaginatedRequestParams | None
|
||||
) -> types.ListResourcesResult:
|
||||
return types.ListResourcesResult(
|
||||
resources=[
|
||||
types.Resource(
|
||||
uri=f"file:///{name}.txt",
|
||||
name=name,
|
||||
title=SAMPLE_RESOURCES[name]["title"],
|
||||
description=f"A sample text resource named {name}",
|
||||
mime_type="text/plain",
|
||||
)
|
||||
for name in SAMPLE_RESOURCES.keys()
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def handle_read_resource(
|
||||
ctx: ServerRequestContext, params: types.ReadResourceRequestParams
|
||||
) -> types.ReadResourceResult:
|
||||
parsed = urlparse(str(params.uri))
|
||||
if not parsed.path:
|
||||
raise ValueError(f"Invalid resource path: {params.uri}")
|
||||
name = parsed.path.replace(".txt", "").lstrip("/")
|
||||
|
||||
if name not in SAMPLE_RESOURCES:
|
||||
raise ValueError(f"Unknown resource: {params.uri}")
|
||||
|
||||
return types.ReadResourceResult(
|
||||
contents=[
|
||||
types.TextResourceContents(
|
||||
uri=str(params.uri),
|
||||
text=SAMPLE_RESOURCES[name]["content"],
|
||||
mime_type="text/plain",
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.option("--port", default=8000, help="Port to listen on for HTTP")
|
||||
@click.option(
|
||||
"--transport",
|
||||
type=click.Choice(["stdio", "streamable-http"]),
|
||||
default="stdio",
|
||||
help="Transport type",
|
||||
)
|
||||
def main(port: int, transport: str) -> int:
|
||||
app = Server(
|
||||
"mcp-simple-resource",
|
||||
on_list_resources=handle_list_resources,
|
||||
on_read_resource=handle_read_resource,
|
||||
)
|
||||
|
||||
if transport == "streamable-http":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app.streamable_http_app(), host="127.0.0.1", port=port)
|
||||
else:
|
||||
from mcp.server.stdio import stdio_server
|
||||
|
||||
async def arun():
|
||||
async with stdio_server() as streams:
|
||||
await app.run(streams[0], streams[1], app.create_initialization_options())
|
||||
|
||||
anyio.run(arun)
|
||||
|
||||
return 0
|
||||
@@ -0,0 +1,43 @@
|
||||
[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 = "Model Context Protocol a Series of LF Projects, LLC." }]
|
||||
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.5", "click>=8.2.0", "httpx>=0.27", "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 = 120
|
||||
target-version = "py310"
|
||||
|
||||
[dependency-groups]
|
||||
dev = ["pyright>=1.1.378", "pytest>=8.3.3", "ruff>=0.6.9"]
|
||||
Reference in New Issue
Block a user