Token导航 LogoToken导航TokenDH.com
Codemode Sqlite MCP logo
数据服务未说明官方级别未说明来源级核验

Codemode Sqlite MCP

MCP Server

一个高性能的SQLite MCP服务器,支持标准MCP模式和创新的代码生成模式(Codemode),通过生成Go代码执行数据库操作,显著提高LLM与数据库交互的效率和性能。

工具数

0

提示词数

0

GitHub Stars

8

资源数

0
SQLite代码生成GoClaude高性能Claude DesktopClaude

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

imran31415

提供方

imran31415

最后核验

2026/5/17 20:20

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

codemode sqlite-mcp

一个高性能的SQLite MCP(模型上下文协议)服务器,具有一种实验性的代码生成方法,称为 编码模式 这使得LLM能够通过生成的Go代码而不是顺序的工具调用来执行数据库操作。

快速入门

安装

go install github.com/imran31415/codemode-sqlite-mcp@latest

或者从源代码构建:

git clone https://github.com/imran31415/codemode-sqlite-mcp.git
cd codemode-sqlite-mcp
go build -o codemode-sqlite-mcp .

基本用法

作为MCP服务器(适用于Claude Desktop):

codemode-sqlite-mcp --mode=stdio --db=./mydata.db

作为HTTP服务器:

codemode-sqlite-mcp --mode=http --port=8084 --db=./mydata.db

作为交互式代码模式代理:

export ANTHROPIC_API_KEY="your-api-key"
codemode-sqlite-mcp --mode=codemode --db=./mydata.db

Claude桌面配置

添加到您的Claude Desktop MCP配置文件中:

{
  "mcpServers": {
    "codemode-sqlite": {
      "command": "codemode-sqlite-mcp",
      "args": ["--mode=stdio", "--db=/path/to/database.db"]
    }
  }
}

______________________________________________________________________

概述

此软件包为LLM数据库交互提供了两种不同的方法:

  1. 标准MCP模式:将SQLite操作公开为LLM按顺序调用的MCP工具
  2. 编码模式:LLM生成完整的Go程序,可在单次通过中执行数据库操作

Codemode方法在复杂数据库操作的令牌使用和延迟方面显示出显著的效率提升。

建筑

┌─────────────────────────────────────────────────────────────┐
│                    codemode-sqlite-mcp                      │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌─────────────┐   ┌─────────────┐   ┌─────────────────┐   │
│  │   MCP       │   │  Codemode   │   │    SQLite       │   │
│  │   Server    │   │    Agent    │   │    Tools        │   │
│  │             │   │             │   │                 │   │
│  │  - stdio    │   │  - LLM API  │   │  - db_info      │   │
│  │  - http     │   │  - Code Gen │   │  - list_tables  │   │
│  │             │   │  - Executor │   │  - query        │   │
│  └──────┬──────┘   └──────┬──────┘   │  - CRUD ops     │   │
│         │                 │          └────────┬────────┘   │
│         └────────┬────────┘                   │            │
│                  │                            │            │
│           ┌──────▼────────────────────────────▼──────┐     │
│           │              Tool Registry               │     │
│           │    (Unified interface for all tools)     │     │
│           └──────────────────────────────────────────┘     │
│                              │                             │
│                    ┌─────────▼─────────┐                   │
│                    │   SQLite Database │                   │
│                    │  (modernc.org/    │                   │
│                    │   sqlite - pure   │                   │
│                    │   Go, no CGO)     │                   │
│                    └───────────────────┘                   │
└─────────────────────────────────────────────────────────────┘

可用工具

该包通过MCP公开了8个SQLite操作:

工具说明
db_info获取数据库元数据(路径、大小、表计数)
list_tables列出数据库中的所有表
get_table_schema获取表的列定义
create_record插入新记录
read_records通过过滤和分页查询记录
update_records更新符合条件的记录
delete_records删除符合条件的记录
query使用参数化值执行任意SQL

Codemode:代码生成方法

运作原理

Codemode代理不进行多次工具调用,而是:

  1. 接受自然语言任务(例如,“通过支出找到前5名客户”)
  2. 生成完成任务的完整Go程序
  3. 在沙盒解释器(yaegi)中执行程序
  4. 返回输出

生成的代码可以访问 registryCall 调用数据库工具的函数:

package main

import "fmt"

func main() {
    result, err := registryCall("query", map[string]interface{}{
        "sql": `SELECT c.Name, SUM(i.Total) as TotalSpent
                FROM customers c
                JOIN invoices i ON c.CustomerId = i.CustomerId
                GROUP BY c.CustomerId
                ORDER BY TotalSpent DESC
                LIMIT 5`,
    })
    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    // Process and format results...
}

为什么要生成代码?

传统的MCP工具调用需要LLM和工具服务器之间的多次往返:

LLM → list_tables → result → LLM → get_schema → result → LLM → query → result → LLM

每次往返都会增加延迟和令牌开销。复杂的操作可能需要5-15次工具调用。

使用Codemode,LLM会预先对整个任务进行推理,并生成一个程序:

LLM → generates code → execute once → result

基准测试结果

复杂多表查询测试(Chinook数据库):

度量标准MCP编码模式改进
成功率100%100%-
平均延迟18.8s9.0s快2.1倍
平均代币109401859减少5.9倍
工具调用平均5.61-

在复杂的分析查询中,令牌节省了83%,延迟减少了52%。

封装结构

codemode-sqlite-mcp/
├── main.go                 # CLI entry point
├── pkg/
│   ├── executor/           # Sandboxed code execution
│   │   ├── executor.go     # Yaegi interpreter wrapper
│   │   ├── preprocessor.go # Code preprocessing (import fixing, etc.)
│   │   └── errors.go       # Error types
│   ├── validator/          # Security validation
│   │   └── validator.go    # Code safety checks
│   ├── tools/              # SQLite operations
│   │   ├── registry.go     # Tool registry
│   │   ├── tools.go        # Tool implementations
│   │   └── types.go        # Type definitions
│   └── mcp/                # MCP protocol
│       ├── server.go       # Core MCP logic
│       ├── stdio.go        # STDIO transport
│       ├── http.go         # HTTP transport
│       └── types.go        # JSON-RPC types
├── codemode/               # Codemode agent
│   ├── agent.go            # LLM integration
│   └── prompts.go          # System prompts
├── examples/               # Usage examples
│   ├── basic/              # Basic library usage
│   ├── embed-server/       # Embedding MCP server
│   └── custom-executor/    # Custom code execution
└── benchmark/              # Performance testing
    ├── chinook.go          # Chinook database scenarios
    └── runner.go           # Benchmark runner

程序化使用

用作图书馆

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/imran31415/codemode-sqlite-mcp/codemode"
    "github.com/imran31415/codemode-sqlite-mcp/pkg/tools"
)

func main() {
    // Initialize database
    if err := tools.InitDB("./mydata.db"); err != nil {
        log.Fatal(err)
    }
    defer tools.CloseDB()

    // Create tool registry
    registry := tools.NewRegistry()

    // Option 1: Use tools directly
    result, err := registry.Call("list_tables", nil)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Tables: %v\n", result)

    // Option 2: Use the Codemode agent
    agent := codemode.NewAgent(registry, codemode.AgentConfig{
        APIKey: "your-anthropic-api-key",
    })

    execResult, err := agent.Execute(context.Background(),
        "List all users and count how many are active")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(execResult.Output)
}

自定义工具注册

registry := tools.NewRegistry()

// Add a custom tool
registry.Register(&tools.ToolInfo{
    Name:        "custom_analytics",
    Description: "Run custom analytics query",
    Parameters: []tools.ParamInfo{
        {Name: "metric", Type: "string", Required: true},
    },
    Function: func(args map[string]interface{}) (interface{}, error) {
        metric := args["metric"].(string)
        // Custom implementation...
        return result, nil
    },
})

嵌入MCP服务器

package main

import (
    "github.com/imran31415/codemode-sqlite-mcp/pkg/mcp"
    "github.com/imran31415/codemode-sqlite-mcp/pkg/tools"
)

func main() {
    tools.InitDB("./data.db")
    defer tools.CloseDB()

    registry := tools.NewRegistry()
    server := mcp.NewServer(registry)

    // Use STDIO transport (for Claude Desktop integration)
    transport := mcp.NewStdioTransport(server)
    transport.Run()

    // Or use HTTP transport
    // httpTransport := mcp.NewHTTPTransport(server, "8084")
    // httpTransport.Run()
}

执行器:沙盒代码执行

遗嘱执行人使用 yaegi,一个Go解释器,安全运行LLM生成的代码:

  • 无需编译步骤
  • 限制标准库访问
  • 注入用于数据库访问的符号
  • 可配置的执行超时
  • 捕获的stdout/stderr
executor := executor.NewExecutor()

// Execute with custom symbols (e.g., database tools)
symbols := map[string]map[string]reflect.Value{
    "tools/tools": {
        "registryCall": reflect.ValueOf(registryCallFunc),
    },
}

result, err := executor.ExecuteWithSymbols(ctx, code, 30*time.Second, symbols)

预处理

执行器包括一个预处理器,它:

  1. 从markdown代码块中提取Go代码
  2. 自动添加缺失的导入
  3. 注入工具注册表符号
  4. 验证基本代码结构

CLI 参考

codemode-sqlite-mcp [OPTIONS]

OPTIONS:
  --mode=MODE       Server mode: stdio, http, or codemode (default: stdio)
  --port=PORT       HTTP port for http mode (default: 8084)
  --db=PATH         Path to SQLite database (default: codemode.db)
  --api-key=KEY     Anthropic API key (required for codemode mode)
  --model=MODEL     LLM model to use (optional)
  --init-db         Initialize database with sample data and exit
  --help            Show help message

MODES:
  stdio     MCP server with stdio transport (for Claude Desktop)
  http      MCP server with HTTP transport
  codemode  Interactive agent with LLM code generation

依赖项

安全考虑

执行者实施了多项安全措施:

  1. 沙盒执行:代码在解释器中运行,而不是作为编译的二进制文件运行
  2. 有限进口:只有安全的标准库包可用
  3. 执行超时:可配置的超时可防止无限循环
  4. 代码验证:执行前的基本结构验证
  5. 无文件系统访问权限:生成的代码无法直接访问文件系统

对于生产使用,建议根据您的安全要求对验证器规则进行额外审查。

局限性

  • 生成的代码仅限于可用的标准库包
  • 复杂的数据转换可能需要多次LLM尝试
  • 解释器比编译的Go慢(可用于数据库I/O绑定任务)
  • 目前仅支持Anthropic API(计划支持OpenAI)

例子

examples/ 目录包含可运行的示例:

# Basic usage - direct tool calls and Codemode agent
go run ./examples/basic

# Embed MCP server in your own HTTP application
go run ./examples/embed-server

# Custom executor with injected symbols
go run ./examples/custom-executor

运行基准

# Build the benchmark tool
go build -o bin/benchmark ./cmd/benchmark/...

# Run Chinook database benchmark
ANTHROPIC_API_KEY="your-key" ./bin/benchmark --mode=chinook

# Run simple comparison benchmark
ANTHROPIC_API_KEY="your-key" ./bin/benchmark --mode=comparison

许可证

MIT许可证-有关详细信息,请参阅许可证文件。

作者

  • 伊姆兰·哈萨纳利
  • 阿尔希恩·阿里

联系人:develop.imran@gmail.com

贡献

欢迎捐款。请在提交pull请求之前打开一个问题来讨论重大更改。

目录标签

目录标签

SQLite代码生成GoClaude高性能本地部署数据库操作LLM集成

支持客户端

Claude DesktopClaude

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

api-key

工具数量(toolCount,工具数)

0

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明api-key部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP