Harden MCP defaults and Nexar auth handling
This commit is contained in:
+12
-12
@@ -21,13 +21,13 @@ class NexarServer:
|
||||
"""MCP server for Nexar API integration (formerly Octopart)"""
|
||||
|
||||
def __init__(self):
|
||||
self.api_token = os.getenv('NEXAR_TOKEN') # Nexar uses tokens instead of API keys
|
||||
self.api_token = os.getenv('NEXAR_TOKEN')
|
||||
self.base_url = "https://api.nexar.com/graphql"
|
||||
|
||||
# Demo mode with realistic data
|
||||
self.demo_mode = not self.api_token
|
||||
if self.demo_mode:
|
||||
print("Info: No NEXAR_TOKEN found, using enhanced demo mode with realistic pricing", file=sys.stderr)
|
||||
print("Info: No NEXAR_TOKEN found, using demo pricing mode", file=sys.stderr)
|
||||
|
||||
async def handle_request(self, request: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Handle MCP requests"""
|
||||
@@ -45,7 +45,7 @@ class NexarServer:
|
||||
"tools": {}
|
||||
},
|
||||
"serverInfo": {
|
||||
"name": "octopart-api",
|
||||
"name": "nexar-api",
|
||||
"version": "1.0.0"
|
||||
}
|
||||
}
|
||||
@@ -165,19 +165,19 @@ class NexarServer:
|
||||
}
|
||||
|
||||
async def _search_parts(self, args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Search for parts using Octopart API"""
|
||||
"""Search for parts using Nexar API"""
|
||||
try:
|
||||
query = args.get('query', '')
|
||||
|
||||
# If we have API key, try real API call
|
||||
if self.api_key and REQUESTS_AVAILABLE:
|
||||
if self.api_token and REQUESTS_AVAILABLE:
|
||||
try:
|
||||
return await self._search_parts_api(args)
|
||||
except Exception as e:
|
||||
print(f"Octopart API call failed, using demo mode: {e}", file=sys.stderr)
|
||||
print(f"Nexar API call failed, using demo mode: {e}", file=sys.stderr)
|
||||
|
||||
# Demo mode - comprehensive pricing examples
|
||||
print("Warning: No Octopart API key, using demo data", file=sys.stderr)
|
||||
print("Warning: No NEXAR_TOKEN configured, using demo data", file=sys.stderr)
|
||||
|
||||
# Create demo responses based on search query
|
||||
demo_parts = []
|
||||
@@ -312,7 +312,7 @@ class NexarServer:
|
||||
"parts": demo_parts,
|
||||
"total_count": len(demo_parts),
|
||||
"demo_mode": True,
|
||||
"message": "Demo data - add OCTOPART_API_KEY for real pricing from all distributors",
|
||||
"message": "Demo data - add NEXAR_TOKEN for live pricing from distributors",
|
||||
"distributors_covered": ["Digi-Key", "Mouser", "Farnell", "Newark", "Arrow", "RS Components", "Avnet"]
|
||||
}
|
||||
|
||||
@@ -320,14 +320,14 @@ class NexarServer:
|
||||
return {"error": f"Search failed: {str(e)}"}
|
||||
|
||||
async def _search_parts_api(self, args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Make actual API call to Octopart"""
|
||||
"""Make actual API call to Nexar"""
|
||||
query = args.get('query', '')
|
||||
limit = args.get('limit', 10)
|
||||
|
||||
endpoint = f"{self.base_url}/parts/search"
|
||||
|
||||
headers = {
|
||||
'Authorization': f'Token {self.api_key}',
|
||||
'Authorization': f'Token {self.api_token}',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
@@ -391,14 +391,14 @@ class NexarServer:
|
||||
"parts": parts,
|
||||
"total_count": len(parts),
|
||||
"demo_mode": False,
|
||||
"message": f"Found {len(parts)} parts via Octopart API",
|
||||
"message": f"Found {len(parts)} parts via Nexar API",
|
||||
"distributors_covered": list(set([d for p in parts for d in p.get('distributors', {}).keys()]))
|
||||
}
|
||||
else:
|
||||
raise Exception(f"API error: {response.status_code} - {response.text}")
|
||||
|
||||
except Exception as e:
|
||||
raise Exception(f"Octopart API call failed: {str(e)}")
|
||||
raise Exception(f"Nexar API call failed: {str(e)}")
|
||||
|
||||
async def _get_part_pricing(self, args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Get comprehensive pricing for a specific part"""
|
||||
|
||||
@@ -3,18 +3,18 @@
|
||||
"component_database": {
|
||||
"command": ["python3", "-m", "mcp_servers.component_db"],
|
||||
"description": "Component database with pricing and availability",
|
||||
"enabled": true
|
||||
"enabled": false
|
||||
},
|
||||
"nexar_api": {
|
||||
"command": ["python3", "-m", "mcp_servers.nexar"],
|
||||
"description": "Nexar API - Multi-distributor pricing in one call (FREE tier: 1K calls/month)",
|
||||
"enabled": true,
|
||||
"requires_api_key": false
|
||||
"requires_api_key": true
|
||||
},
|
||||
"kicad_tools": {
|
||||
"command": ["python3", "-m", "mcp_servers.kicad_tools"],
|
||||
"description": "KiCad design manipulation tools",
|
||||
"enabled": true
|
||||
"enabled": false
|
||||
}
|
||||
},
|
||||
"ai_enhancement": {
|
||||
|
||||
Reference in New Issue
Block a user