JoshuaC215 / agent-service-toolkit

Full toolkit for running an AI agent service built with LangGraph, FastAPI and Streamlit
https://agent-service-toolkit.streamlit.app
MIT License
516 stars 89 forks source link

problem with connection to database #113

Open Dominik-Majcherczyk opened 18 hours ago

Dominik-Majcherczyk commented 18 hours ago

Hi! I created a minimalist tool that is supposed to connect to my local database (maria db, localhost). The database works - I am able to connect to it from a test file. Unfortunately, inside the application, the tool is unable to find the specific database. Does anyone know what could be the problem? Could it possibly be due to the docker?

image

minimalist tool:

def list_tables_func(query: str = "") -> str:
    """Lists all tables in the connected MariaDB database.

    Useful for when you need to know what tables are available in the database.
    Returns a simple list of table names.

    Args:
        query (str): Ignored, kept for compatibility with tool interface.

    Returns:
        str: Newline-separated list of table names.
    """
    try:
        connection = mysql.connector.connect(
        host="172.17.0.1",
        port=3306,
        user="root",
        password="root"
    )

        cursor = connection.cursor()
        cursor.execute("SHOW TABLES")
        tables = cursor.fetchall()

        return "\n".join(table[0] for table in tables)

    except Exception as e:
        raise ValueError(
            f"list_tables() raised error: {e}."
            " Please check database connection and credentials"
        )

    finally:
        if 'cursor' in locals():
            cursor.close()
        if 'connection' in locals() and connection.is_connected():
            connection.close()
Dominik-Majcherczyk commented 18 hours ago

test file which is working perfectly fine:


def test_db_connection():
    try:
        connection = mysql.connector.connect(
            host="172.17.0.1",
            port=3306,
            user="root",
            password="root"
        )

        if connection.is_connected():
            print("Successfully connected to the database!")

            # Test querying tables
            cursor = connection.cursor()
            cursor.execute("SHOW TABLES")
            tables = cursor.fetchall()

            print("\nTables in database:")
            for table in tables:
                print(f"- {table[0]}")

    except Exception as e:
        print(f"Error: {e}")

    finally:
        if 'cursor' in locals():
            cursor.close()
        if 'connection' in locals() and connection.is_connected():
            connection.close()
            print("\nDatabase connection closed.")

if __name__ == "__main__":
    test_db_connection()
JoshuaC215 commented 12 hours ago

Yes, if you're running the service in docker and running the test file directly in local python, then the docker networking is probably the issue.

You could validate that by running the service in local python too. See the instructions here.