Skip to content

Instantly share code, notes, and snippets.

@grahama1970
Last active February 20, 2025 18:26
Show Gist options
  • Save grahama1970/98c5cd8bc4e266fd7b3ebad36e6823eb to your computer and use it in GitHub Desktop.
Save grahama1970/98c5cd8bc4e266fd7b3ebad36e6823eb to your computer and use it in GitHub Desktop.
This README outlines the limitations of the Cursor MCP Environment, highlighting constraints on package access and environment variables. Key issues include reliance on the Python standard library and the need to hardcode sensitive data. Workarounds involve adapting scripts accordingly. Open questions focus on potential configurations for improv…

Cursor MCP Environment Limitations

Discovered Constraints

  1. Package Access

    • Cannot access project's virtual environment
    • Limited to Python standard library
    • No ability to install additional packages
  2. Environment Variables

    • Cannot access project's .env files
    • Cannot access system environment variables
    • Requires hardcoding sensitive data (not recommended)

Current Workarounds

  1. Package Dependencies

    • Rewrite scripts to use only standard library
    • Example: Using urllib.request instead of requests
  2. API Keys/Secrets

    • Currently requires hardcoding in script
    • Security concern that needs better solution

Open Questions

  1. Is there a way to configure MCP's Python environment?
  2. Can MCP be configured to access project's virtual environment?
  3. Is there a secure way to handle API keys and secrets?
{
"mcpServers": {
"echo": {
// Usage: /echo
"command": "echo",
"args": ["{\"message\": \"Hello from MCP\"}"],
"description": "Simple hardcoded echo test with JSON"
},
"brave_search": {
// Usage: /brave_search As of now, What is the top news item today?
"command": "python",
"args": [
"-c",
"import sys, os, json; global input_data; input_data = json.dumps({'query': ' '.join(sys.argv[1:])}); exec(open('${workspaceFolder}/src/summarizer/mcp/brave_search_mcp.py').read())"
],
"cwd": "${workspaceFolder}",
"description": "Using -c flag and the Brave Search API and ditching the venv as MCP seems to have its own venv which is not particularly useful"
}
}
import json
import sys
import os
import logging
from dotenv import load_dotenv
import requests
from urllib.parse import quote
import urllib.request
import urllib.parse
# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def brave_search(query, api_key):
"""Queries the Brave Search API and returns the results."""
logger.debug("ENTERING BRAVE_SEARCH FUNCTION")
logger.debug(f"QUERY: {query}")
logger.debug(f"API KEY STARTS WITH: {api_key[:4]}")
url = f"https://api.search.brave.com/res/v1/web/search?q={urllib.parse.quote(query)}"
logger.debug(f"URL: {url}")
headers = {
"Accept": "application/json",
"Accept-Encoding": "gzip",
"X-Subscription-Token": api_key,
}
logger.debug(f"HEADERS: {headers}")
try:
logger.debug("ABOUT TO MAKE REQUEST")
req = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(req) as response:
logger.debug(f"GOT RESPONSE STATUS: {response.status}")
logger.debug(f"RESPONSE HEADERS: {response.headers}")
logger.debug(f"RESPONSE CONTENT: {response.read()[:500]}") # First 500 bytes
return json.loads(response.read())
except Exception as e:
logger.error(f"API REQUEST FAILED WITH ERROR: {str(e)}")
return {"error": str(e)}
# Main execution
query = input_data.get("query")
try:
from dotenv import load_dotenv
load_dotenv()
api_key = "123456"
# can't seem to use env variable here
# api_key = os.getenv("BRAVE_SEARCH_API_KEY")
logger.debug(f"API key: {api_key}")
logger.info("MCP script starting")
if not api_key:
print(json.dumps({"error": "BRAVE_SEARCH_API_KEY not found in environment"}))
else:
results = brave_search(query, api_key)
print(json.dumps(results))
except Exception as e:
print(json.dumps({"error": str(e)}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment