-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-docs.py
136 lines (118 loc) · 4.12 KB
/
google-docs.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from typing import Any, Dict, List, Optional, Callable
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from googleapiclient.http import BatchHttpRequest
from mcp.server.fastmcp import FastMCP
import os.path
import pickle
# Initialize FastMCP server
mcp = FastMCP("google-docs")
# If modifying these scopes, delete the file token.pickle.
SCOPES = [
'https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/drive'
]
def get_credentials() -> Credentials:
"""Get valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
"""
creds = None
# Use the user's home directory for token storage
token_path = os.path.join(os.path.expanduser('~'), '.google-docs-token.pickle')
if os.path.exists(token_path):
with open(token_path, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open(token_path, 'wb') as token:
pickle.dump(creds, token)
return creds
def get_docs_service():
"""Get the Google Docs service instance."""
creds = get_credentials()
return build('docs', 'v1', credentials=creds)
def get_drive_service():
"""Get the Google Drive service instance."""
creds = get_credentials()
return build('drive', 'v3', credentials=creds)
# Document Management Tools
@mcp.tool()
async def get_document(document_id: str) -> Dict[str, Any]:
"""Get a Google Doc by its ID.
Args:
document_id: The ID of the document to retrieve
"""
try:
service = get_docs_service()
doc = service.documents().get(documentId=document_id).execute()
return {
"success": True,
"document_id": doc.get('documentId'),
"title": doc.get('title'),
"body": doc.get('body'),
"revision_id": doc.get('revisionId')
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
@mcp.tool()
async def create_document(title: str, content: str = "") -> Dict[str, Any]:
"""Create a new Google Doc with the specified title and content.
Args:
title: The title of the new document
content: Optional initial content for the document
"""
try:
# First create the document in Drive
drive_service = get_drive_service()
file_metadata = {
'name': title,
'mimeType': 'application/vnd.google-apps.document'
}
file = drive_service.files().create(body=file_metadata).execute()
# If content is provided, update the document
if content:
docs_service = get_docs_service()
requests = [
{
'insertText': {
'location': {
'index': 1
},
'text': content
}
}
]
docs_service.documents().batchUpdate(
documentId=file['id'],
body={'requests': requests}
).execute()
return {
"success": True,
"document_id": file['id'],
"title": title,
"message": "Document created successfully"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
if __name__ == "__main__":
try:
# Run the MCP server
print("🚀 Starting Google Docs MCP server...")
mcp.run(transport='sse')
except Exception as e:
print(f"❌ Error: {str(e)}")
exit(1)