-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmcp_integration_example.py
More file actions
189 lines (145 loc) · 8.04 KB
/
Copy pathmcp_integration_example.py
File metadata and controls
189 lines (145 loc) · 8.04 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
"""
MCP Server Integration Example
Shows how python-alfresco-mcp-server can integrate with the FIXED python-alfresco-api
wrapper clients instead of using low-level HTTP calls.
KEY BENEFITS:
- Automatic URL construction (no more manual URL building)
- Built-in authentication handling
- Cleaner, more maintainable code
- Future-proof against API changes
"""
from datetime import datetime
from python_alfresco_api import ClientFactory
from python_alfresco_api.auth_util import SimpleAuthUtil
class MCPAlfrescoIntegration:
"""Example integration class showing how MCP servers can use wrapper clients."""
def __init__(self, base_url: str = "http://localhost:8080", username: str = "admin", password: str = "admin"):
"""Initialize with wrapper clients."""
print(f"🔧 Initializing MCP integration with fixed wrapper clients...")
# Create wrapper clients with fixed URL architecture
auth = SimpleAuthUtil(username, password)
factory = ClientFactory(base_url=base_url, auth_util=auth, verify_ssl=False)
self.core_client = factory.create_core_client()
self.search_client = factory.create_search_client()
# Get the properly configured httpx clients
self.core_http = self.core_client.get_httpx_client()
self.search_http = self.search_client.get_httpx_client()
print(f"✅ Core client configured: {self.core_client.base_url}")
print(f"✅ Search client configured: {self.search_client.base_url}")
# MCP Tool Implementation Examples
def mcp_search_content(self, query: str, max_results: int = 20) -> dict:
"""MCP Tool: search_content - Using wrapper client approach."""
print(f"🔍 MCP search_content: {query}")
search_data = {
"query": {
"query": query,
"language": "afts"
},
"paging": {"maxItems": max_results}
}
# OLD WAY (manual URL construction):
# url = f"{base_url}/alfresco/api/-default-/public/search/versions/1/search"
# NEW WAY (wrapper client with correct base URL):
response = self.search_http.post("/search", json=search_data, timeout=10)
if response.status_code == 200:
results = response.json()
entries = results.get("list", {}).get("entries", [])
print(f"✅ Found {len(entries)} results")
return {"status": "success", "count": len(entries), "results": entries}
else:
print(f"❌ Search failed: {response.status_code}")
return {"status": "error", "code": response.status_code}
def mcp_get_node_properties(self, node_id: str) -> dict:
"""MCP Tool: get_properties - Using wrapper client approach."""
print(f"🏷️ MCP get_properties: {node_id}")
# OLD WAY (manual URL construction):
# url = f"{base_url}/alfresco/api/-default-/public/alfresco/versions/1/nodes/{node_id}"
# NEW WAY (wrapper client with correct base URL):
response = self.core_http.get(f"/nodes/{node_id}?include=properties", timeout=10)
if response.status_code == 200:
node_data = response.json()
properties = node_data.get("entry", {}).get("properties", {})
print(f"✅ Retrieved {len(properties)} properties")
return {"status": "success", "properties": properties, "node": node_data["entry"]}
else:
print(f"❌ Get properties failed: {response.status_code}")
return {"status": "error", "code": response.status_code}
def mcp_update_node_properties(self, node_id: str, properties: dict) -> dict:
"""MCP Tool: set_properties - Using wrapper client approach with defensive handling."""
print(f"✏️ MCP set_properties: {node_id}")
update_data = {"properties": properties}
# OLD WAY (manual URL construction):
# url = f"{base_url}/alfresco/api/-default-/public/alfresco/versions/1/nodes/{node_id}"
# NEW WAY (wrapper client with correct base URL + defensive body handling):
response = self.core_http.put(f"/nodes/{node_id}", json=update_data, timeout=10)
if response.status_code == 200:
result = response.json()
print(f"✅ Properties updated successfully")
return {"status": "success", "node": result["entry"]}
else:
print(f"❌ Update properties failed: {response.status_code}")
return {"status": "error", "code": response.status_code}
def mcp_upload_document(self, filename: str, content: bytes, parent_id: str = "-root-") -> dict:
"""MCP Tool: upload_document - Using wrapper client approach."""
print(f"📤 MCP upload_document: {filename}")
files = {'filedata': (filename, content, 'application/octet-stream')}
data = {'name': filename, 'nodeType': 'cm:content'}
# OLD WAY (manual URL construction):
# url = f"{base_url}/alfresco/api/-default-/public/alfresco/versions/1/nodes/{parent_id}/children"
# NEW WAY (wrapper client with correct base URL):
response = self.core_http.post(f"/nodes/{parent_id}/children", files=files, data=data, timeout=30)
if response.status_code == 201:
result = response.json()
file_info = result["entry"]
print(f"✅ Upload successful: {file_info['id']}")
return {"status": "success", "file_id": file_info['id'], "file": file_info}
else:
print(f"❌ Upload failed: {response.status_code}")
return {"status": "error", "code": response.status_code}
def demo_mcp_integration():
"""Demonstrate the improved MCP integration."""
print("🚀 MCP SERVER INTEGRATION DEMO")
print("=" * 50)
print("Comparing OLD vs NEW approach for MCP servers")
# Initialize integration
mcp = MCPAlfrescoIntegration()
print(f"\n📋 COMPARISON SUMMARY:")
print("=" * 30)
print("🔴 OLD APPROACH (Manual URL Construction):")
print(" ❌ Manual URL building: base_url + '/alfresco/api/...'")
print(" ❌ Hardcoded API paths throughout code")
print(" ❌ Manual authentication header management")
print(" ❌ Error-prone and brittle")
print("\n🟢 NEW APPROACH (Wrapper Clients):")
print(" ✅ Automatic URL construction via wrapper clients")
print(" ✅ Built-in authentication handling")
print(" ✅ Defensive body serialization")
print(" ✅ Relative paths: /nodes/xyz, /search")
print(" ✅ Future-proof against API changes")
print(f"\n💡 MIGRATION STRATEGY:")
print(" 1. Replace manual base_url construction")
print(" 2. Use wrapper_client.get_httpx_client() for HTTP operations")
print(" 3. Use relative paths instead of full URLs")
print(" 4. Keep existing JSON data structures")
print(f"\n🎯 EXAMPLE CODE CHANGES:")
print("OLD:")
print(' url = f"{base_url}/alfresco/api/-default-/public/alfresco/versions/1/nodes/{node_id}"')
print(' response = httpx.post(url, json=data, auth=auth)')
print("\nNEW:")
print(' core_client = factory.create_core_client()')
print(' http = core_client.get_httpx_client() # Pre-configured with auth & base URL')
print(' response = http.post(f"/nodes/{node_id}", json=data) # Relative path!')
print(f"\n🏆 BENEFITS FOR python-alfresco-mcp-server:")
print(" • Reduced code complexity")
print(" • Improved maintainability")
print(" • Automatic handling of URL architecture changes")
print(" • Better error handling")
print(" • Cleaner, more professional codebase")
return mcp
if __name__ == "__main__":
# Run the demo
mcp_integration = demo_mcp_integration()
print(f"\n🎉 MCP INTEGRATION READY!")
print("Your python-alfresco-mcp-server can now use wrapper clients!")
print("All 17 MCP tools will work with the fixed architecture!")