-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
53 lines (44 loc) · 2.22 KB
/
Copy pathserver.py
File metadata and controls
53 lines (44 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
1) The @mcp.tool() decorator: This is important. When you add it to a regular Python function, FastMCP checks the function name, type hints (like title: str), and the docstring. It then puts all this into a JSON schema and gives it to the LLM.
2) The Docstrings: Notice how descriptive the docstrings are. Don’t skip these. The LLM reads your docstrings to decide when and how to use the tool. If your docstring is vague, the AI might make up parameters or not call the tool at all.
3) STDIO Transport: mcp.run(transport= “stdio”) tells the script to use your terminal’s standard input and output instead of opening a web port. This is great for local agents on your machine because it avoids network firewall issues.
"""
from mcp.server.fastmcp import FastMCP
# 1. Initialize the FastMCP server with a descriptive name
mcp = FastMCP("LocalTaskManager")
# 2. Set up a simple in-memory database
# In a real-world scenario, you'd replace this with SQLite or PostgreSQL.
tasks = []
current_id = 1
# 3. Define our first Tool: Adding a task
@mcp.tool()
def add_task(title: str, description: str = "") -> str:
"""
Add a new task to the user's task list.
The AI will read this docstring to understand what the tool does.
"""
global current_id
tasks.append({
"id": current_id,
"title": title,
"description": description,
"status": "pending"
})
current_id += 1
return f"Success! Task '{title}' was added to the tracker."
# 4. Define our second Tool: Listing pending tasks
@mcp.tool()
def get_pending_tasks() -> str:
"""Retrieve all pending tasks so the user knows what to do next."""
pending = [t for t in tasks if t["status"] == "pending"]
if not pending:
return "You're all caught up! No pending tasks right now."
output = "Here are your pending tasks:\n"
for t in pending:
output += f"- [ID: {t['id']}] {t['title']} (Details: {t['description']})\n"
return output
# 5. Run the server
if __name__ == "__main__":
# We use "stdio" transport. This means the AI client will communicate
# with this Python script via standard input and output streams.
mcp.run(transport="stdio")