w
This commit is contained in:
+69
-54
@@ -7,15 +7,24 @@ import json
|
||||
|
||||
from mcp.server.fastmcp import Context
|
||||
|
||||
from owa_mcp.server import mcp, AppContext
|
||||
from owa_mcp.owa_client import OWAClient
|
||||
from owa_mcp.server import AppContext, mcp
|
||||
from owa_mcp.server_lifecycle import require_client
|
||||
|
||||
|
||||
_DISTINGUISHED_NAMES = {
|
||||
"msgfolderroot", "inbox", "sentitems", "drafts", "deleteditems",
|
||||
"junkemail", "outbox", "calendar", "contacts", "tasks", "notes",
|
||||
"journal", "searchfolders",
|
||||
"msgfolderroot",
|
||||
"inbox",
|
||||
"sentitems",
|
||||
"drafts",
|
||||
"deleteditems",
|
||||
"junkemail",
|
||||
"outbox",
|
||||
"calendar",
|
||||
"contacts",
|
||||
"tasks",
|
||||
"notes",
|
||||
"journal",
|
||||
"searchfolders",
|
||||
}
|
||||
|
||||
_HEADER_TZ = {
|
||||
@@ -68,36 +77,40 @@ def check_session(ctx: Context = None) -> str:
|
||||
"__type": "FolderResponseShape:#Exchange",
|
||||
"BaseShape": "Default",
|
||||
},
|
||||
"FolderIds": [
|
||||
{"__type": "DistinguishedFolderId:#Exchange", "Id": "inbox"}
|
||||
],
|
||||
"FolderIds": [{"__type": "DistinguishedFolderId:#Exchange", "Id": "inbox"}],
|
||||
},
|
||||
}
|
||||
|
||||
try:
|
||||
data = client.request("GetFolder", payload)
|
||||
except Exception as e:
|
||||
return json.dumps({
|
||||
"authenticated": False,
|
||||
"error": str(e),
|
||||
"cookie_file": str(client.cookie_file),
|
||||
})
|
||||
return json.dumps(
|
||||
{
|
||||
"authenticated": False,
|
||||
"error": str(e),
|
||||
"cookie_file": str(client.cookie_file),
|
||||
}
|
||||
)
|
||||
|
||||
for msg in client.extract_items(data):
|
||||
if "Folders" in msg:
|
||||
folder = msg["Folders"][0]
|
||||
return json.dumps({
|
||||
"authenticated": True,
|
||||
"mailbox": folder.get("DisplayName", ""),
|
||||
"unread": folder.get("UnreadCount", 0),
|
||||
"cookie_file": str(client.cookie_file),
|
||||
})
|
||||
return json.dumps(
|
||||
{
|
||||
"authenticated": True,
|
||||
"mailbox": folder.get("DisplayName", ""),
|
||||
"unread": folder.get("UnreadCount", 0),
|
||||
"cookie_file": str(client.cookie_file),
|
||||
}
|
||||
)
|
||||
|
||||
return json.dumps({
|
||||
"authenticated": False,
|
||||
"error": "Unexpected response",
|
||||
"cookie_file": str(client.cookie_file),
|
||||
})
|
||||
return json.dumps(
|
||||
{
|
||||
"authenticated": False,
|
||||
"error": "Unexpected response",
|
||||
"cookie_file": str(client.cookie_file),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
@@ -159,13 +172,15 @@ def get_folders(
|
||||
for msg in client.extract_items(data):
|
||||
if "RootFolder" in msg and "Folders" in msg["RootFolder"]:
|
||||
for f in msg["RootFolder"]["Folders"]:
|
||||
folders.append({
|
||||
"name": f.get("DisplayName", "Unknown"),
|
||||
"id": f.get("FolderId", {}).get("Id", ""),
|
||||
"total_count": f.get("TotalCount", 0),
|
||||
"unread_count": f.get("UnreadCount", 0),
|
||||
"child_folder_count": f.get("ChildFolderCount", 0),
|
||||
})
|
||||
folders.append(
|
||||
{
|
||||
"name": f.get("DisplayName", "Unknown"),
|
||||
"id": f.get("FolderId", {}).get("Id", ""),
|
||||
"total_count": f.get("TotalCount", 0),
|
||||
"unread_count": f.get("UnreadCount", 0),
|
||||
"child_folder_count": f.get("ChildFolderCount", 0),
|
||||
}
|
||||
)
|
||||
|
||||
return json.dumps(folders, ensure_ascii=False)
|
||||
|
||||
@@ -216,11 +231,13 @@ def create_folder(
|
||||
for msg in client.extract_items(data):
|
||||
if "Folders" in msg:
|
||||
folder = msg["Folders"][0]
|
||||
return json.dumps({
|
||||
"success": True,
|
||||
"name": name,
|
||||
"id": folder.get("FolderId", {}).get("Id", ""),
|
||||
})
|
||||
return json.dumps(
|
||||
{
|
||||
"success": True,
|
||||
"name": name,
|
||||
"id": folder.get("FolderId", {}).get("Id", ""),
|
||||
}
|
||||
)
|
||||
|
||||
return json.dumps({"error": "Unexpected response", "raw": str(data)})
|
||||
|
||||
@@ -280,11 +297,13 @@ def rename_folder(
|
||||
for msg in client.extract_items(data):
|
||||
if "Folders" in msg:
|
||||
folder = msg["Folders"][0]
|
||||
return json.dumps({
|
||||
"success": True,
|
||||
"new_name": new_name,
|
||||
"id": folder.get("FolderId", {}).get("Id", ""),
|
||||
})
|
||||
return json.dumps(
|
||||
{
|
||||
"success": True,
|
||||
"new_name": new_name,
|
||||
"id": folder.get("FolderId", {}).get("Id", ""),
|
||||
}
|
||||
)
|
||||
|
||||
return json.dumps({"error": "Unexpected response", "raw": str(data)})
|
||||
|
||||
@@ -316,9 +335,7 @@ def empty_folder(
|
||||
"Header": _HEADER_TZ,
|
||||
"Body": {
|
||||
"__type": "EmptyFolderRequest:#Exchange",
|
||||
"FolderIds": [
|
||||
{"__type": "FolderId:#Exchange", "Id": folder_id}
|
||||
],
|
||||
"FolderIds": [{"__type": "FolderId:#Exchange", "Id": folder_id}],
|
||||
"DeleteType": delete_type,
|
||||
"DeleteSubFolders": delete_sub_folders,
|
||||
"SuppressReadReceipt": True,
|
||||
@@ -362,9 +379,7 @@ def delete_folder(
|
||||
"Header": _HEADER_TZ,
|
||||
"Body": {
|
||||
"__type": "DeleteFolderRequest:#Exchange",
|
||||
"FolderIds": [
|
||||
{"__type": "FolderId:#Exchange", "Id": folder_id}
|
||||
],
|
||||
"FolderIds": [{"__type": "FolderId:#Exchange", "Id": folder_id}],
|
||||
"DeleteType": delete_type,
|
||||
},
|
||||
}
|
||||
@@ -405,9 +420,7 @@ def move_folder(
|
||||
"Header": _HEADER_TZ,
|
||||
"Body": {
|
||||
"__type": "MoveFolderRequest:#Exchange",
|
||||
"FolderIds": [
|
||||
{"__type": "FolderId:#Exchange", "Id": folder_id}
|
||||
],
|
||||
"FolderIds": [{"__type": "FolderId:#Exchange", "Id": folder_id}],
|
||||
"ToFolderId": {
|
||||
"__type": "TargetFolderId:#Exchange",
|
||||
"BaseFolderId": _folder_id_dict(target_parent_folder_id),
|
||||
@@ -423,9 +436,11 @@ def move_folder(
|
||||
for msg in client.extract_items(data):
|
||||
if "Folders" in msg:
|
||||
folder = msg["Folders"][0]
|
||||
return json.dumps({
|
||||
"success": True,
|
||||
"folder_id": folder.get("FolderId", {}).get("Id", ""),
|
||||
})
|
||||
return json.dumps(
|
||||
{
|
||||
"success": True,
|
||||
"folder_id": folder.get("FolderId", {}).get("Id", ""),
|
||||
}
|
||||
)
|
||||
|
||||
return json.dumps({"error": "Unexpected response", "raw": str(data)})
|
||||
|
||||
Reference in New Issue
Block a user