docs: cover the remaining Tier 1 audit items (#3325)
This commit is contained in:
parent
2a1cc94493
commit
9057285683
3 changed files with 247 additions and 1 deletions
25
docs_src/prompts/tutorial004.py
Normal file
25
docs_src/prompts/tutorial004.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from pathlib import Path
|
||||
|
||||
from mcp.server import MCPServer
|
||||
from mcp.server.mcpserver import Message, UserMessage
|
||||
from mcp.types import EmbeddedResource, TextResourceContents
|
||||
|
||||
mcp = MCPServer("Code Helper")
|
||||
|
||||
STYLE_GUIDE_FILE = Path(__file__).parent / "style-guide.md" # or the path to your file on disk
|
||||
|
||||
|
||||
@mcp.resource("style://python", mime_type="text/markdown")
|
||||
def style_guide() -> str:
|
||||
"""The team's Python style guide."""
|
||||
return STYLE_GUIDE_FILE.read_text(encoding="utf-8")
|
||||
|
||||
|
||||
@mcp.prompt()
|
||||
def review_code(code: str) -> list[Message]:
|
||||
"""Review a piece of code against the team style guide."""
|
||||
guide = TextResourceContents(uri="style://python", mime_type="text/markdown", text=style_guide())
|
||||
return [
|
||||
UserMessage(EmbeddedResource(resource=guide)),
|
||||
UserMessage(f"Review this code against the style guide above:\n\n{code}"),
|
||||
]
|
||||
17
docs_src/prompts/tutorial005.py
Normal file
17
docs_src/prompts/tutorial005.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from pathlib import Path
|
||||
|
||||
from mcp.server import MCPServer
|
||||
from mcp.server.mcpserver import Image, Message, UserMessage
|
||||
|
||||
mcp = MCPServer("Code Helper")
|
||||
|
||||
DIAGRAM_FILE = Path(__file__).parent / "architecture.png" # or the path to your file on disk
|
||||
|
||||
|
||||
@mcp.prompt()
|
||||
def explain_component(component: str) -> list[Message]:
|
||||
"""Explain one component using the architecture diagram."""
|
||||
return [
|
||||
UserMessage(Image(path=DIAGRAM_FILE)),
|
||||
UserMessage(f"Where does {component} sit in this architecture, and what does it talk to?"),
|
||||
]
|
||||
28
docs_src/prompts/tutorial006.py
Normal file
28
docs_src/prompts/tutorial006.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from contextlib import suppress
|
||||
|
||||
from mcp.server import MCPServer
|
||||
from mcp.server.mcpserver import Context
|
||||
from mcp.server.mcpserver.prompts import Prompt
|
||||
|
||||
mcp = MCPServer("Code Helper")
|
||||
|
||||
|
||||
@mcp.prompt()
|
||||
def review_code(code: str) -> str:
|
||||
"""Review a piece of code."""
|
||||
return f"Please review this code:\n\n{code}"
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def save_template(name: str, instruction: str, ctx: Context) -> str:
|
||||
"""Save an instruction as a prompt the user can pick from the menu."""
|
||||
|
||||
def template(code: str) -> str:
|
||||
return f"{instruction}\n\n{code}"
|
||||
|
||||
with suppress(ValueError): # replace an existing entry of the same name
|
||||
mcp.remove_prompt(name)
|
||||
mcp.add_prompt(Prompt.from_function(template, name=name, description=instruction))
|
||||
await ctx.notify_prompts_changed()
|
||||
await ctx.session.send_prompt_list_changed()
|
||||
return f"Saved '{name}' to the prompt menu."
|
||||
Loading…
Reference in a new issue