Repository navigation
Expand file tree
/
Copy pathtools.py
More file actions
97 lines (86 loc) · 5.1 KB
/
Copy pathtools.py
File metadata and controls
97 lines (86 loc) · 5.1 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
import json
import logging
from typing import Any, Dict, Optional
from pydantic import Field
from config import Config
from http_client import HttpClient
logger = logging.getLogger(__name__)
def join(base: str, path: str) -> str:
return f"{base.rstrip('/')}/{path.lstrip('/')}"
def get_api_id_tool(config: Config, http: HttpClient):
def get_api_id(
company_name: str = Field(description="Name of the company to look up and lowercase (e.g. 'stripe', 'auth0')")
) -> str:
"""Get the API ID for a company from the mapping dictionary.
Args:
company_name: Name of the company to look up and lowercase (e.g. "stripe", "auth0")
Returns:
API ID if found, otherwise "null". If the API ID is not found, please check the company name and try again. If problem persists, the API ID is not available in SuperIndex MCP, therefore the API/SDK information is not available in SuperIndex MCP.
"""
print(f"get_api_id_tool: {company_name}")
cleaned = (company_name or "").strip().lower()
if not cleaned:
return "null"
url = join(config.url_path, "/mcp/api_id")
error_message = "Please try another name. If you see this message, API/SDK information is currently not available in SuperIndex MCP. Kindly request the API/SDK information to be added to SuperIndex MCP at https://github.com/sampleapp-ai/superindex"
try:
resp = http.post_json_with_retry(url, {"company_name": cleaned})
if resp.status_code != 200:
logger.warning("get_api_id non-200: %s %s", resp.status_code, resp.text)
return error_message
data = resp.json()
if data.get("api_id") == "null":
return error_message
else:
print("api_id", data.get("api_id", "null"))
return str(data.get("api_id", "null"))
except (json.JSONDecodeError, Exception) as e:
logger.error("get_api_id error: %s", e)
return error_message
return get_api_id
def get_api_info_tool(config: Config, http: HttpClient):
def get_api_info(
api_id: str = Field(description="The API identifier (namespace). If the API ID is not found, please check the company name and try again. If problem persists, the API ID is not available in SuperIndex MCP, therefore the API/SDK information is not available in SuperIndex MCP. Do not make up an API ID."),
user_query: str = Field(description="User's query about the API/SDK with the API/SDK version installed (e.g. 'Basic auth and setup for auth0 version 4.0 sdk')"),
language: str = Field("typescript", description="Programming language (e.g., 'typescript')"),
framework: str = Field("nextjs", description="Framework being used (e.g., 'nextjs', 'vite')"),
top_k: int = Field(10, description="Number of results to return (default: 10, max: 20)",ge=5,le=15),
) -> str:
"""Get API information by calling the API endpoint with the provided parameters.
Args:
api_id: The API identifier (namespace). If the API ID is not found, please check the company name and try again. If problem persists, the API ID is not available in SuperIndex MCP, therefore the API/SDK information is not available in SuperIndex MCP. Do not make up an API ID.
user_query: User's query about the API/SDK with the API/SDK version installed (e.g. 'Basic auth and setup for auth0 version 4.0 sdk')
language: Programming language (e.g., "typescript")
framework: Framework being used (e.g., "nextjs", "vite")
top_k: Number of results to return (default: 10, max: 20)
Returns:
Combined text content from API matches or error message
"""
error_message = "Please try another name. If you see this message, API/SDK information is currently not available in SuperIndex MCP. Kindly request the API/SDK information to be added to SuperIndex MCP at https://github.com/sampleapp-ai/superindex"
if not api_id or api_id == "null":
return "Error: Invalid API ID provided. Please check the API ID and try again. If problem persists, the API/SDK information is not available in SuperIndex MCP."
try:
k = int(top_k)
if k <= 0:
k = 10
except (TypeError, ValueError):
k = 10
payload: Dict[str, Any] = {
"api_id": api_id,
"query": f"{(user_query or '').strip()} using this {framework} and {language}",
"top_k": k,
}
url = join(config.url_path, "/mcp")
try:
resp = http.post_json_with_retry(url, payload)
if resp.status_code != 200:
logger.warning("get_api_info non-200: %s %s", resp.status_code, resp.text)
return error_message
try:
data = resp.json()
return str(data.get("markdown", ""))
except json.JSONDecodeError:
return resp.text
except Exception as e:
return error_message
return get_api_info