Skip to content

Nem 321 SQL execution follow-up#124

Open
annav1asova wants to merge 2 commits intomainfrom
NEM-321-sql-execution-follow-up
Open

Nem 321 SQL execution follow-up#124
annav1asova wants to merge 2 commits intomainfrom
NEM-321-sql-execution-follow-up

Conversation

@annav1asova
Copy link
Collaborator

MVP for running SQL queries using MCP server

Currently we wrap the synchronous run_sql in asyncio.to_thread to avoid “cannot create event loop inside an existing loop” errors. Each call creates a new thread and a local event loop, which is inefficient under load
Ideally, run_sql should be fully async and reuse a single connection/event loop

Copy link
Collaborator

@JulienArzul JulienArzul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hsestupin what are the plans for the MCP server? Is it worth it to add new tools in this one? or should we stop adding anything here since I imagine it will be re-worked in the common-cli repo anyway?

read_only: bool = True,
):
ds = DatasourceId.from_string_repr(datasource_id)
res = await asyncio.to_thread(self._databao_context_engine.run_sql, ds, sql, read_only=read_only)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a code perspective (without knowing the full specifics of how asyncio works in Python), it feels wrong to me that we need to accommodate calling a synchronous method in a new thread here, because somewhere down the calling chain, something has an async facade and wants to create an event loop in the thread

In theory, I think we should either:

  • make everything async properly (make all plugin methods async)
  • or better hide the Postgres async behaviour: the thread needed to run the method should be created within the Postgres async facade (when catching that an event loop can't be created in the current thread? or maybe there is an other way of making the calls to pgasync synchronous without taking up a full thread's event loop)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed this briefly with @annav1asova .

I don't think we should make changes in the higher abstraction only because some low level custom implementation (postgres plugin) has async bindings. I believe there should be some way to handle this inside Postgres Plugin.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes agreed, that's what I was trying to explain in the second option
=> the deferral to a new thread (ie. calling await asyncio.to_thread) should belong directly in the Postgres plugin somewhere

annotations=ToolAnnotations(readOnlyHint=False, idempotentHint=False, openWorldHint=False),
)
async def run_sql_tool(
datasource_id: str,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, we probably need to add a new tool in the MCP server that allows to list all existing datasources, so that the LLM can figure out what the id they want to use is.

And maybe we could also accept a None ID, in cases the project has only one datasource configured

@hsestupin
Copy link
Collaborator

@hsestupin what are the plans for the MCP server? Is it worth it to add new tools in this one? or should we stop adding anything here since I imagine it will be re-worked in the common-cli repo anyway?

Well, while we definitely should not put any efforts in maintaining CLI commands in this repository, I'm not 100% sure about the MCP server destiny. I think it's a viable plan to provide some [mcp] extra in our library to allow users to run MCP via python API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants