[Python] Integrating Spiceai MCP with Slack Bot

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

    I followed the Spice.ai documentation and the getting started guide to use Spice MCP with OpenAI. Now, I am trying to use it with a Slack bot, but I keep getting 404 errors or "method not found" errors. I believe this might be due to not fully understanding how spice chat receives and responds.

    Here's the code I am using for my Slack bot:

    import os
    import requests
    from slack_bolt import App
    from slack_bolt.adapter.socket_mode import SocketModeHandler
    from dotenv import load_dotenv

    # Load env variables
    load_dotenv()

    app = App(
    token=os.environ["SLACK_BOT_TOKEN"],
    signing_secret=os.environ["SLACK_SIGNING_SECRET"]
    )

    SPICE_API_URL = os.getenv("SPICE_API_URL", "http://localhost:8090/v1/chat/completions")

    @app.event("app_mention")
    def handle_app_mention_events(body, say, logger):
    text = body["event"]["text"]
    channel_id = body["event"]["channel"]

    allowed_channels = os.environ["SLACK_ALLOWED_CHANNELS"].split(",")
    if channel_id not in allowed_channels:
    logger.info(f"Ignored message from unauthorized channel: {channel_id}")
    return

    query_text = text.split(">", 1)[1].strip() if ">" in text else text.strip()
    logger.info(f"Received message: {query_text}")

    try:
    response = requests.post(
    SPICE_API_URL,
    json={
    "model": "openai", # This must match your model in the MCP config
    "messages": [
    {"role": "user", "content": query_text}
    ]
    }
    )

    response.raise_for_status()
    result = response.json()
    message = result["choices"][0]["message"]["content"]
    say(message)

    except Exception as e:
    logger.exception("Failed to fetch response from Spice MCP")
    say(f"Error: {str(e)}")

    if __name__ == "__main__":
    handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
    handler.start()


    I'm getting 404 or "method not found" errors when I try to make the request to the SPICE_API_URL. I checked for the spice chat endpoint URL but didn't find much documentation on it. Just looking for any clue in the right direction from the community.

    Continue reading...

Compartilhe esta Página