Enhanced examples: (#1)

- Standardized comments in examples to pure English for consistency.
- Added a minimalist example [example/nacos_simple_example.py] to simplify user implementation.
- Included an SSE port configuration demo to facilitate fixing port conflicts.
This commit is contained in:
fly 2025-05-16 13:44:21 +08:00 committed by GitHub
parent 345571626a
commit 75f7543e46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 18 deletions

View File

@ -1,34 +1,30 @@
from nacos_mcp_wrapper.server.nacos_mcp import NacosMCP
from nacos_mcp_wrapper.server.nacos_settings import NacosSettings
# Create an MCP server
# mcp = FastMCP("Demo")
# Create an MCP server instance
nacos_settings = NacosSettings()
nacos_settings.SERVER_ADDR = "<nacos_server_addr> e.g. 127.0.0.1:8848"
mcp = NacosMCP("nacos-mcp-python",nacos_settings=nacos_settings)
# Add an addition tool
nacos_settings.SERVER_ADDR = "127.0.0.1:8848" # <nacos_server_addr> e.g. 127.0.0.1:8848
mcp = NacosMCP("nacos-mcp-python", nacos_settings=nacos_settings, port=18001)
# Register an addition tool
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
"""Add two integers together"""
return a + b
# Register a subtraction tool
@mcp.tool()
def minus(a: int, b: int) -> int:
"""Subtract two numbers"""
return a - b
# Register a prompt function
@mcp.prompt()
def get_prompt(topic: str) -> str:
"""Get a personalized greeting"""
return f"Hello, {topic}!"
@mcp.resource("greeting://{name}")
def get_resource(name: str) -> str:
"""Get a file"""
return f"Hello, {name}!"
# Add a dynamic greeting resource
# Register a dynamic resource endpoint
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
"""Get a personalized greeting"""
@ -37,5 +33,5 @@ def get_greeting(name: str) -> str:
if __name__ == "__main__":
try:
mcp.run(transport="sse")
except ValueError as e:
print(f"运行时发生错误: {e}")
except Exception as e:
print(f"Runtime error: {e}")

View File

@ -22,16 +22,17 @@ async def fetch_website(
@click.command()
@click.option("--port", default=8000, help="Port to listen on for SSE")
@click.option("--port", default=18002, help="Port to listen on for SSE")
@click.option("--server_addr", default="127.0.0.1:8848", help="Nacos server address")
@click.option(
"--transport",
type=click.Choice(["stdio", "sse"]),
default="sse",
help="Transport type",
)
def main(port: int, transport: str) -> int:
def main(port: int, transport: str, server_addr: str) -> int:
nacos_settings = NacosSettings()
nacos_settings.SERVER_ADDR = "<nacos_server_addr> e.g. 127.0.0.1:8848"
nacos_settings.SERVER_ADDR = server_addr
# app = Server("mcp-website-fetcher")
app = NacosServer("mcp-website-fetcher",nacos_settings=nacos_settings)
@ -76,7 +77,7 @@ def main(port: int, transport: str) -> int:
async with sse.connect_sse(
request.scope, request.receive, request._send
) as streams:
# 0 进 1出
# 0 input stream, 1 output stream
await app.run(
streams[0], streams[1], app.create_initialization_options()
)

View File

@ -0,0 +1,28 @@
import click
from nacos_mcp_wrapper.server.nacos_mcp import NacosMCP
from nacos_mcp_wrapper.server.nacos_settings import NacosSettings
from datetime import datetime
@click.command()
@click.option("--port", default=18003, help="Port to listen on for SSE")
@click.option("--name", default="nacos-simple-mcp", help="The name of the MCP service")
@click.option("--server_addr", default="127.0.0.1:8848", help="Nacos server address")
def main(port: int, name: str, server_addr: str):
# Registration settings for Nacos
nacos_settings = NacosSettings()
nacos_settings.SERVER_ADDR = server_addr
mcp = NacosMCP(name=name, nacos_settings=nacos_settings, port=port)
@mcp.tool()
def get_datetime() -> str:
"""Get current datetime as string"""
return datetime.now().isoformat() # 返回字符串格式的时间
try:
mcp.run(transport="sse")
except ValueError as e:
print(f"Runtime errors: {e}")
if __name__ == "__main__":
main()