-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (49 loc) · 2.05 KB
/
main.py
File metadata and controls
61 lines (49 loc) · 2.05 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
54
55
56
57
58
59
60
61
from mcp.server.fastmcp import FastMCP
from typing import List
# In-memory mock database with 20 leave days to start
employee_leaves = {
"E001": {"balance": 18, "history": ["2024-12-25", "2025-01-01"]},
"E002": {"balance": 20, "history": []}
}
# Create MCP server
mcp = FastMCP("LeaveManager")
# Tool: Check Leave Balance
@mcp.tool()
def get_leave_balance(employee_id: str) -> str:
"""Check how many leave days are left for the employee"""
data = employee_leaves.get(employee_id)
if data:
return f"{employee_id} has {data['balance']} leave days remaining."
return "Employee ID not found."
# Tool: Apply for Leave with specific dates
@mcp.tool()
def apply_leave(employee_id: str, leave_dates: List[str]) -> str:
"""
Apply leave for specific dates (e.g., ["2025-04-17", "2025-05-01"])
"""
if employee_id not in employee_leaves:
return "Employee ID not found."
requested_days = len(leave_dates)
available_balance = employee_leaves[employee_id]["balance"]
if available_balance < requested_days:
return f"Insufficient leave balance. You requested {requested_days} day(s) but have only {available_balance}."
# Deduct balance and add to history
employee_leaves[employee_id]["balance"] -= requested_days
employee_leaves[employee_id]["history"].extend(leave_dates)
return f"Leave applied for {requested_days} day(s). Remaining balance: {employee_leaves[employee_id]['balance']}."
# Resource: Leave history
@mcp.tool()
def get_leave_history(employee_id: str) -> str:
"""Get leave history for the employee"""
data = employee_leaves.get(employee_id)
if data:
history = ', '.join(data['history']) if data['history'] else "No leaves taken."
return f"Leave history for {employee_id}: {history}"
return "Employee ID not found."
# Resource: Greeting
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
"""Get a personalized greeting"""
return f"Hello, {name}! How can I assist you with leave management today?"
if __name__ == "__main__":
mcp.run()