Accessing SQLite Database with Spring AI MCP
Case 2: Accessing SQLite Database with Spring AI MCP
This agent enables natural language interaction with an SQLite database through a command-line interface.
You can view the complete source code for this example.
Running the Example
Prerequisites
-
Install uvx (Universal Package Manager): First, ensure that npm is installed on your local machine, then run the following command:
Terminal window npm install -g npx -
Download the example source code
Terminal window git clone https://github.com/springaialibaba/spring-ai-alibaba-examples.gitcd spring-ai-alibaba-examples/spring-ai-alibaba-mcp-example/spring-ai-alibaba-mcp-manual-example/sqlite/ai-mcp-sqlite-chatbot -
Set environment variables
Terminal window # Tongyi LLM Dashscope API-KEYexport AI_DASHSCOPE_API_KEY=${your-api-key-here} -
Build the example
Terminal window ./mvnw clean install
Running the Sample Application
Run the example to query data in the database:
./mvnw spring-boot:run
Enter the content you want to query to perform database queries:
USER: What is the sum of all product prices?ASSISTANT: The sum of all product prices is 1642.8 yuan.
More complex queries are also supported:
USER: Tell me which products have prices higher than the averageASSISTANT:The following products have prices higher than the average:
1. Smart Watch, priced at 199.99 yuan2. Wireless Earbuds, priced at 89.99 yuan3. Mini Drone, priced at 299.99 yuan4. Keyboard, priced at 129.99 yuan5. Gaming Headset, priced at 159.99 yuan6. Fitness Tracker, priced at 119.99 yuan7. Portable SSD, priced at 179.99 yuan
Example Architecture (Source Code Explanation)
Initializing McpClient
@Bean(destroyMethod = "close")public McpSyncClient mcpClient() {
var stdioParams = ServerParameters.builder("uvx") .args("mcp-server-sqlite", "--db-path", getDbPath()) .build();
var mcpClient = McpClient.sync(new StdioServerTransport(stdioParams), Duration.ofSeconds(10), new ObjectMapper());
var init = mcpClient.initialize();
System.out.println("MCP Initialized: " + init);
return mcpClient;}
In this code:
-
A separate process is created using the uvx package management tool to run the mcp-server-sqlite service.
-
A stdio-based transport layer is created to communicate with the MCP server run by uvx.
-
SQLite is specified as the backend database along with its location, a timeout of 10 seconds is set for operations, and Jackson is used for JSON serialization. Finally, the connection to the MCP server is initialized.
Function Callbacks
Register MCP tools through Spring AI:
@Beanpublic List<McpFunctionCallback> functionCallbacks(McpSyncClient mcpClient) { return mcpClient.listTools(null) .tools() .stream() .map(tool -> new McpFunctionCallback(mcpClient, tool)) .toList();}
In this code:
-
Available MCP clients are obtained through mcpClient.
-
MCP clients are converted to Spring AI’s Function Callbacks.
-
These Function Callbacks are registered with ChatClient.