Skip to content

Instantly share code, notes, and snippets.

@nicolas-martin
Last active July 30, 2025 19:07
Show Gist options
  • Save nicolas-martin/66946807e6f97f0525dd6e2b756ea47a to your computer and use it in GitHub Desktop.
Save nicolas-martin/66946807e6f97f0525dd6e2b756ea47a to your computer and use it in GitHub Desktop.
import os
import asyncio
from datetime import datetime, timedelta, timezone
from mcp_use import MCPClient
async def main():
client = MCPClient.from_config_file("multi_mcp_config.json")
try:
# compute cutoff ISO timestamp
cutoff = datetime.now(timezone.utc) - timedelta(minutes=5)
cutoff_iso = cutoff.isoformat() # e.g. '2025-07-30T14:25:00+00:00'
# 1. fetch messages after cutoff
recent_msgs = await client.call_tool(
server_name="discord",
tool_name="read_messages",
params={
"channel_id": os.getenv("CHANNEL_ID"),
"after": cutoff_iso, # only messages in last 5m
"limit": 1000
}
)
# 2. filter by USER_ID in Python
user_id = os.getenv("USER_ID")
user_msgs = [
m for m in recent_msgs
if m.get("author", {}).get("id") == user_id
]
# 3. upsert into Postgres
for m in user_msgs:
content = m["content"].replace("'", "''")
sql = (
"INSERT INTO user_messages (id, author_id, content, ts) "
f"VALUES ('{m['id']}', '{user_id}', '{content}', '{m['timestamp']}') "
"ON CONFLICT DO NOTHING"
)
await client.call_tool(
server_name="postgres",
tool_name="execute_query",
params={"sql": sql}
)
# 4. score each category directly
category_scores = await client.call_tool(
server_name="openai",
tool_name="create_chat_completion",
params={
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": (
"Based on these user messages, assign scores 1-10 for each category "
"(meme, technical, builder, helpful, random) for this user. "
"Return JSON with category names as keys and scores as values."
)
},
{"role": "user", "content": str(user_msgs)}
]
}
)
print("Category scores:", category_scores)
# 5. update user scores in postgres
for category, score in category_scores.items():
sql = (
f"INSERT INTO user_category_scores (user_id, category, score, updated_at) "
f"VALUES ('{user_id}', '{category}', {score}, NOW()) "
f"ON CONFLICT (user_id, category) DO UPDATE SET "
f"score = {score}, updated_at = NOW()"
)
await client.call_tool(
server_name="postgres",
tool_name="execute_query",
params={"sql": sql}
)
finally:
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(main())
{
"mcpServers": {
"discord": {
"command": "mcp-discord",
"args": [],
"env": {
"DISCORD_TOKEN": "$DISCORD_TOKEN"
}
},
"postgres": {
"command": "postgres-mcp",
"args": [
"--access-mode=unrestricted"
],
"env": {
"DATABASE_URI": "$DATABASE_URL"
}
},
"openai": {
"command": "python",
"args": [
"-m",
"src.mcp_server_openai.server"
],
"env": {
"PYTHONPATH": "/path/to/mcp-server-openai",
"OPENAI_API_KEY": "$OPENAI_API_KEY"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment