Repository navigation
Expand file tree
/
Copy pathapply_query_first.py
More file actions
110 lines (93 loc) · 4.43 KB
/
Copy pathapply_query_first.py
File metadata and controls
110 lines (93 loc) · 4.43 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
#!/usr/bin/env python3
"""Apply query-first pattern to all API Credential OAuth implementations."""
from pathlib import Path
ROOT = Path(__file__).resolve().parent
PLATFORM_TOKEN_EXEMPTION = "acedatacloud"
OLD_CODE = ''' if not application_id:
return None
cred_resp = await client.post(
f"{settings.platform_base_url}/api/v1/credentials/",
headers={**headers, "Content-Type": "application/json"},
json={
"application_id": application_id,
"name": "OAuth MCP",
},
)
if cred_resp.status_code not in (200, 201):
logger.error(
"Failed to get managed MCP Credential: "
f"{cred_resp.status_code} {cred_resp.text[:500]}"
)
return None
data = cred_resp.json()
token = data.get("token") if isinstance(data, dict) else None
if isinstance(token, str) and token:
logger.info(f"Using shared OAuth MCP Credential (id={data.get('id')})")
return token
logger.error("Managed MCP Credential response did not contain a token")'''
NEW_CODE = ''' if not application_id:
return None
# Query for existing shared "OAuth MCP" credential first
cred_list_resp = await client.get(
f"{settings.platform_base_url}/api/v1/credentials/",
params={"application_id": application_id, "name": "OAuth MCP"},
headers=headers,
)
existing_creds = []
if cred_list_resp.status_code == 200:
cred_data = cred_list_resp.json()
existing_creds = cred_data.get("items", cred_data.get("results", []))
# Reuse existing credential if found
if existing_creds and isinstance(existing_creds, list):
data = existing_creds[0]
token = data.get("token") if isinstance(data, dict) else None
if isinstance(token, str) and token:
logger.info(
f"Reusing existing OAuth MCP Credential (id={data.get('id')})"
)
return token
# Create new credential only if not found
cred_resp = await client.post(
f"{settings.platform_base_url}/api/v1/credentials/",
headers={**headers, "Content-Type": "application/json"},
json={
"application_id": application_id,
"name": "OAuth MCP",
},
)
if cred_resp.status_code not in (200, 201):
logger.error(
"Failed to create OAuth MCP Credential: "
f"{cred_resp.status_code} {cred_resp.text[:500]}"
)
return None
data = cred_resp.json()
token = data.get("token") if isinstance(data, dict) else None
if isinstance(token, str) and token:
logger.info(f"Created new OAuth MCP Credential (id={data.get('id')})")
return token
logger.error("OAuth MCP Credential response did not contain a token")'''
def main():
updated = []
for path in sorted(ROOT.glob("*/core/oauth.py")):
name = path.parts[-3]
if name == PLATFORM_TOKEN_EXEMPTION:
continue
source = path.read_text()
if "_get_user_credential" not in source:
continue
# Skip if already has query-first pattern
if 'params={"application_id": application_id, "name": "OAuth MCP"}' in source:
print(f"✓ {name}: already has query-first pattern")
continue
if OLD_CODE not in source:
print(f"✗ {name}: old pattern not found")
continue
# Apply transformation
new_source = source.replace(OLD_CODE, NEW_CODE)
path.write_text(new_source)
updated.append(name)
print(f"✓ {name}: applied query-first pattern")
print(f"\nUpdated {len(updated)} MCPs: {', '.join(updated)}")
if __name__ == "__main__":
main()