Once you’ve mastered the basics of MCP, you can start building more powerful and secure integrations. This guide covers advanced concepts and provides realistic examples.
Security: Handling API Keys and Secrets
You should never hardcode sensitive information like API keys, tokens, or database credentials directly into your MCP server’s code. The correct way to handle secrets is by using environment variables.
Flexus allows you to define environment variables in the MCP Server configuration. The MCP Agent will then securely pass them to your server process when it’s launched.
How to Use Environment Variables
In the Flexus UI: When creating or editing your MCP Server, find the “Environment Variables” section. Add a new variable, for example:
Key: JIRA_API_KEY
Value: your-secret-api-key-here
In your MCP Server code (Python example): Access the variable using your language’s standard library for reading environment variables.
import os
import sys
import json
# --- Tool implementation ---
defget_jira_issue(issue_id):
api_key = os.getenv("JIRA_API_KEY")
ifnot api_key:
return {"error": "JIRA_API_KEY is not configured."}
# Your logic to connect to Jira using the API key...
This approach ensures that your secrets are kept out of your code and are managed securely within the Flexus platform.
Real-World Examples
Example 1: Jira Integration
Here is a more complete example of an MCP server that connects to Jira.
Jira MCP Server
Functionality:
Provides a get_jira_issue tool.
Reads the Jira URL and API key from environment variables.
Use Case:
Allows a bot to retrieve details about a Jira ticket and use that information in a conversation or workflow. For example, a bot could answer the question, “What’s the status of ticket PROJ-123?”
return {"error": "Jira API key or URL not configured."}
try:
response = requests.get(
f"{jira_url}/rest/api/2/issue/{issue_id}",
headers={"Authorization": f"Bearer {api_key}"}
)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
return {"error": str(e)}
# --- Main server loop (simplified) ---
# Remember to implement the full stdio loop as shown in the basics guide.
Example 2: Database Connection
This example shows how to safely query a PostgreSQL database. The connection string is passed as an environment variable.
Database MCP Server
Functionality:
Provides a query_customer_by_email tool.
Connects to a database using credentials from environment variables.
Use Case:
A bot can retrieve customer information from a production database without having direct access itself. For example, it could answer, “What is the subscription status for the user with email ‘test@example.com’?”
cur.execute("SELECT status, plan FROM subscriptions WHERE email = %s;", (email,))
row = cur.fetchone()
if row:
return {"status": row[0], "plan": row[1]}
else:
return {"error": "Customer not found."}
exceptExceptionas e:
return {"error": str(e)}
finally:
if'conn'inlocals() and conn:
conn.close()
# --- Main server loop (simplified) ---
# Remember to implement the full stdio loop.
These advanced patterns demonstrate the power and flexibility of MCP. By externalizing complex logic and securely managing credentials, you can build robust, scalable, and maintainable bots.