mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-04-09 17:36:23 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa362c77da | ||
|
|
1641eaf278 | ||
|
|
200a03c225 |
407
pkg/resolvemcp/README.md
Normal file
407
pkg/resolvemcp/README.md
Normal file
@@ -0,0 +1,407 @@
|
|||||||
|
# resolvemcp
|
||||||
|
|
||||||
|
Package `resolvemcp` exposes registered database models as **Model Context Protocol (MCP) tools and resources** over HTTP/SSE transport. It mirrors the `resolvespec` package patterns — same model registration API, same filter/sort/pagination/preload options, same lifecycle hook system.
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/bitechdev/ResolveSpec/pkg/resolvemcp"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 1. Create a handler
|
||||||
|
handler := resolvemcp.NewHandlerWithGORM(db, resolvemcp.Config{
|
||||||
|
BaseURL: "http://localhost:8080",
|
||||||
|
})
|
||||||
|
|
||||||
|
// 2. Register models
|
||||||
|
handler.RegisterModel("public", "users", &User{})
|
||||||
|
handler.RegisterModel("public", "orders", &Order{})
|
||||||
|
|
||||||
|
// 3. Mount routes
|
||||||
|
r := mux.NewRouter()
|
||||||
|
resolvemcp.SetupMuxRoutes(r, handler)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Config
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Config struct {
|
||||||
|
// BaseURL is the public-facing base URL of the server (e.g. "http://localhost:8080").
|
||||||
|
// Sent to MCP clients during the SSE handshake so they know where to POST messages.
|
||||||
|
// If empty, it is detected from each incoming request using the Host header and
|
||||||
|
// TLS state (X-Forwarded-Proto is honoured for reverse-proxy deployments).
|
||||||
|
BaseURL string
|
||||||
|
|
||||||
|
// BasePath is the URL path prefix where MCP endpoints are mounted (e.g. "/mcp").
|
||||||
|
// Required.
|
||||||
|
BasePath string
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Handler Creation
|
||||||
|
|
||||||
|
| Function | Description |
|
||||||
|
|---|---|
|
||||||
|
| `NewHandlerWithGORM(db *gorm.DB, cfg Config) *Handler` | Backed by GORM |
|
||||||
|
| `NewHandlerWithBun(db *bun.DB, cfg Config) *Handler` | Backed by Bun |
|
||||||
|
| `NewHandlerWithDB(db common.Database, cfg Config) *Handler` | Backed by any `common.Database` |
|
||||||
|
| `NewHandler(db common.Database, registry common.ModelRegistry, cfg Config) *Handler` | Full control over registry |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Registering Models
|
||||||
|
|
||||||
|
```go
|
||||||
|
handler.RegisterModel(schema, entity string, model interface{}) error
|
||||||
|
```
|
||||||
|
|
||||||
|
- `schema` — database schema name (e.g. `"public"`), or empty string for no schema prefix.
|
||||||
|
- `entity` — table/entity name (e.g. `"users"`).
|
||||||
|
- `model` — a pointer to a struct (e.g. `&User{}`).
|
||||||
|
|
||||||
|
Each call immediately creates four MCP **tools** and one MCP **resource** for the model.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## HTTP / SSE Transport
|
||||||
|
|
||||||
|
The `*server.SSEServer` returned by any of the helpers below implements `http.Handler`, so it works with every Go HTTP framework.
|
||||||
|
|
||||||
|
`Config.BasePath` is required and used for all route registration.
|
||||||
|
`Config.BaseURL` is optional — when empty it is detected from each request.
|
||||||
|
|
||||||
|
### Gorilla Mux
|
||||||
|
|
||||||
|
```go
|
||||||
|
resolvemcp.SetupMuxRoutes(r, handler)
|
||||||
|
```
|
||||||
|
|
||||||
|
Registers:
|
||||||
|
|
||||||
|
| Route | Method | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `{BasePath}/sse` | GET | SSE connection — clients subscribe here |
|
||||||
|
| `{BasePath}/message` | POST | JSON-RPC — clients send requests here |
|
||||||
|
| `{BasePath}/*` | any | Full SSE server (convenience prefix) |
|
||||||
|
|
||||||
|
### bunrouter
|
||||||
|
|
||||||
|
```go
|
||||||
|
resolvemcp.SetupBunRouterRoutes(router, handler)
|
||||||
|
```
|
||||||
|
|
||||||
|
Registers `GET {BasePath}/sse` and `POST {BasePath}/message` on the provided `*bunrouter.Router`.
|
||||||
|
|
||||||
|
### Gin (or any `http.Handler`-compatible framework)
|
||||||
|
|
||||||
|
Use `handler.SSEServer()` to get an `http.Handler` and wrap it with the framework's adapter:
|
||||||
|
|
||||||
|
```go
|
||||||
|
sse := handler.SSEServer()
|
||||||
|
|
||||||
|
// Gin
|
||||||
|
engine.Any("/mcp/*path", gin.WrapH(sse))
|
||||||
|
|
||||||
|
// net/http
|
||||||
|
http.Handle("/mcp/", sse)
|
||||||
|
|
||||||
|
// Echo
|
||||||
|
e.Any("/mcp/*", echo.WrapHandler(sse))
|
||||||
|
```
|
||||||
|
|
||||||
|
### Authentication
|
||||||
|
|
||||||
|
Add middleware before the MCP routes. The handler itself has no auth layer.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## MCP Tools
|
||||||
|
|
||||||
|
### Tool Naming
|
||||||
|
|
||||||
|
```
|
||||||
|
{operation}_{schema}_{entity} // e.g. read_public_users
|
||||||
|
{operation}_{entity} // e.g. read_users (when schema is empty)
|
||||||
|
```
|
||||||
|
|
||||||
|
Operations: `read`, `create`, `update`, `delete`.
|
||||||
|
|
||||||
|
### Read Tool — `read_{schema}_{entity}`
|
||||||
|
|
||||||
|
Fetch one or many records.
|
||||||
|
|
||||||
|
| Argument | Type | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `id` | string | Primary key value. Omit to return multiple records. |
|
||||||
|
| `limit` | number | Max records per page (recommended: 10–100). |
|
||||||
|
| `offset` | number | Records to skip (offset-based pagination). |
|
||||||
|
| `cursor_forward` | string | PK of the **last** record on the current page (next-page cursor). |
|
||||||
|
| `cursor_backward` | string | PK of the **first** record on the current page (prev-page cursor). |
|
||||||
|
| `columns` | array | Column names to include. Omit for all columns. |
|
||||||
|
| `omit_columns` | array | Column names to exclude. |
|
||||||
|
| `filters` | array | Filter objects (see [Filtering](#filtering)). |
|
||||||
|
| `sort` | array | Sort objects (see [Sorting](#sorting)). |
|
||||||
|
| `preloads` | array | Relation preload objects (see [Preloading](#preloading)). |
|
||||||
|
|
||||||
|
**Response:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"data": [...],
|
||||||
|
"metadata": {
|
||||||
|
"total": 100,
|
||||||
|
"filtered": 100,
|
||||||
|
"count": 10,
|
||||||
|
"limit": 10,
|
||||||
|
"offset": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Create Tool — `create_{schema}_{entity}`
|
||||||
|
|
||||||
|
Insert one or more records.
|
||||||
|
|
||||||
|
| Argument | Type | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `data` | object \| array | Single object or array of objects to insert. |
|
||||||
|
|
||||||
|
Array input runs inside a single transaction — all succeed or all fail.
|
||||||
|
|
||||||
|
**Response:**
|
||||||
|
```json
|
||||||
|
{ "success": true, "data": { ... } }
|
||||||
|
```
|
||||||
|
|
||||||
|
### Update Tool — `update_{schema}_{entity}`
|
||||||
|
|
||||||
|
Partially update an existing record. Only non-null, non-empty fields in `data` are applied; existing values are preserved for omitted fields.
|
||||||
|
|
||||||
|
| Argument | Type | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `id` | string | Primary key of the record. Can also be included inside `data`. |
|
||||||
|
| `data` | object (required) | Fields to update. |
|
||||||
|
|
||||||
|
**Response:**
|
||||||
|
```json
|
||||||
|
{ "success": true, "data": { ...merged record... } }
|
||||||
|
```
|
||||||
|
|
||||||
|
### Delete Tool — `delete_{schema}_{entity}`
|
||||||
|
|
||||||
|
Delete a record by primary key. **Irreversible.**
|
||||||
|
|
||||||
|
| Argument | Type | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `id` | string (required) | Primary key of the record to delete. |
|
||||||
|
|
||||||
|
**Response:**
|
||||||
|
```json
|
||||||
|
{ "success": true, "data": { ...deleted record... } }
|
||||||
|
```
|
||||||
|
|
||||||
|
### Resource — `{schema}.{entity}`
|
||||||
|
|
||||||
|
Each model is also registered as an MCP resource with URI `schema.entity` (or just `entity` when schema is empty). Reading the resource returns up to 100 records as `application/json`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Filtering
|
||||||
|
|
||||||
|
Pass an array of filter objects to the `filters` argument:
|
||||||
|
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{ "column": "status", "operator": "=", "value": "active" },
|
||||||
|
{ "column": "age", "operator": ">", "value": 18, "logic_operator": "AND" },
|
||||||
|
{ "column": "role", "operator": "in", "value": ["admin", "editor"], "logic_operator": "OR" }
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Supported Operators
|
||||||
|
|
||||||
|
| Operator | Aliases | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `=` | `eq` | Equal |
|
||||||
|
| `!=` | `neq`, `<>` | Not equal |
|
||||||
|
| `>` | `gt` | Greater than |
|
||||||
|
| `>=` | `gte` | Greater than or equal |
|
||||||
|
| `<` | `lt` | Less than |
|
||||||
|
| `<=` | `lte` | Less than or equal |
|
||||||
|
| `like` | | SQL LIKE (case-sensitive) |
|
||||||
|
| `ilike` | | SQL ILIKE (case-insensitive) |
|
||||||
|
| `in` | | Value in list |
|
||||||
|
| `is_null` | | Column IS NULL |
|
||||||
|
| `is_not_null` | | Column IS NOT NULL |
|
||||||
|
|
||||||
|
### Logic Operators
|
||||||
|
|
||||||
|
- `"logic_operator": "AND"` (default) — filter is AND-chained with the previous condition.
|
||||||
|
- `"logic_operator": "OR"` — filter is OR-grouped with the previous condition.
|
||||||
|
|
||||||
|
Consecutive OR filters are grouped into a single `(cond1 OR cond2 OR ...)` clause.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Sorting
|
||||||
|
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{ "column": "created_at", "direction": "desc" },
|
||||||
|
{ "column": "name", "direction": "asc" }
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Pagination
|
||||||
|
|
||||||
|
### Offset-Based
|
||||||
|
|
||||||
|
```json
|
||||||
|
{ "limit": 20, "offset": 40 }
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cursor-Based
|
||||||
|
|
||||||
|
Cursor pagination uses a SQL `EXISTS` subquery for stable, efficient paging. Always pair with a `sort` argument.
|
||||||
|
|
||||||
|
```json
|
||||||
|
// Next page: pass the PK of the last record on the current page
|
||||||
|
{ "cursor_forward": "42", "limit": 20, "sort": [{"column": "id", "direction": "asc"}] }
|
||||||
|
|
||||||
|
// Previous page: pass the PK of the first record on the current page
|
||||||
|
{ "cursor_backward": "23", "limit": 20, "sort": [{"column": "id", "direction": "asc"}] }
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Preloading Relations
|
||||||
|
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{ "relation": "Profile" },
|
||||||
|
{ "relation": "Orders" }
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
Available relations are listed in each tool's description. Only relations defined on the model struct are valid.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Hook System
|
||||||
|
|
||||||
|
Hooks let you intercept and modify CRUD operations at well-defined lifecycle points.
|
||||||
|
|
||||||
|
### Hook Types
|
||||||
|
|
||||||
|
| Constant | Fires |
|
||||||
|
|---|---|
|
||||||
|
| `BeforeHandle` | After model resolution, before operation dispatch (all CRUD) |
|
||||||
|
| `BeforeRead` / `AfterRead` | Around read queries |
|
||||||
|
| `BeforeCreate` / `AfterCreate` | Around insert |
|
||||||
|
| `BeforeUpdate` / `AfterUpdate` | Around update |
|
||||||
|
| `BeforeDelete` / `AfterDelete` | Around delete |
|
||||||
|
|
||||||
|
### Registering Hooks
|
||||||
|
|
||||||
|
```go
|
||||||
|
handler.Hooks().Register(resolvemcp.BeforeCreate, func(ctx *resolvemcp.HookContext) error {
|
||||||
|
// Inject a timestamp before insert
|
||||||
|
if data, ok := ctx.Data.(map[string]interface{}); ok {
|
||||||
|
data["created_at"] = time.Now()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
// Register the same hook for multiple events
|
||||||
|
handler.Hooks().RegisterMultiple(
|
||||||
|
[]resolvemcp.HookType{resolvemcp.BeforeCreate, resolvemcp.BeforeUpdate},
|
||||||
|
auditHook,
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### HookContext Fields
|
||||||
|
|
||||||
|
| Field | Type | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `Context` | `context.Context` | Request context |
|
||||||
|
| `Handler` | `*Handler` | The resolvemcp handler |
|
||||||
|
| `Schema` | `string` | Database schema name |
|
||||||
|
| `Entity` | `string` | Entity/table name |
|
||||||
|
| `Model` | `interface{}` | Registered model instance |
|
||||||
|
| `Options` | `common.RequestOptions` | Parsed request options (read operations) |
|
||||||
|
| `Operation` | `string` | `"read"`, `"create"`, `"update"`, or `"delete"` |
|
||||||
|
| `ID` | `string` | Primary key from request (read/update/delete) |
|
||||||
|
| `Data` | `interface{}` | Input data (create/update — modifiable) |
|
||||||
|
| `Result` | `interface{}` | Output data (set by After hooks) |
|
||||||
|
| `Error` | `error` | Operation error, if any |
|
||||||
|
| `Query` | `common.SelectQuery` | Live query object (available in `BeforeRead`) |
|
||||||
|
| `Tx` | `common.Database` | Database/transaction handle |
|
||||||
|
| `Abort` | `bool` | Set to `true` to abort the operation |
|
||||||
|
| `AbortMessage` | `string` | Error message returned when aborting |
|
||||||
|
| `AbortCode` | `int` | Optional status code for the abort |
|
||||||
|
|
||||||
|
### Aborting an Operation
|
||||||
|
|
||||||
|
```go
|
||||||
|
handler.Hooks().Register(resolvemcp.BeforeDelete, func(ctx *resolvemcp.HookContext) error {
|
||||||
|
ctx.Abort = true
|
||||||
|
ctx.AbortMessage = "deletion is disabled"
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Managing Hooks
|
||||||
|
|
||||||
|
```go
|
||||||
|
registry := handler.Hooks()
|
||||||
|
registry.HasHooks(resolvemcp.BeforeCreate) // bool
|
||||||
|
registry.Clear(resolvemcp.BeforeCreate) // remove hooks for one type
|
||||||
|
registry.ClearAll() // remove all hooks
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Context Helpers
|
||||||
|
|
||||||
|
Request metadata is threaded through `context.Context` during handler execution. Hooks and custom tools can read it:
|
||||||
|
|
||||||
|
```go
|
||||||
|
schema := resolvemcp.GetSchema(ctx)
|
||||||
|
entity := resolvemcp.GetEntity(ctx)
|
||||||
|
tableName := resolvemcp.GetTableName(ctx)
|
||||||
|
model := resolvemcp.GetModel(ctx)
|
||||||
|
modelPtr := resolvemcp.GetModelPtr(ctx)
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also set values manually (e.g. in middleware):
|
||||||
|
|
||||||
|
```go
|
||||||
|
ctx = resolvemcp.WithSchema(ctx, "tenant_a")
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Adding Custom MCP Tools
|
||||||
|
|
||||||
|
Access the underlying `*server.MCPServer` to register additional tools:
|
||||||
|
|
||||||
|
```go
|
||||||
|
mcpServer := handler.MCPServer()
|
||||||
|
mcpServer.AddTool(myTool, myHandler)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Table Name Resolution
|
||||||
|
|
||||||
|
The handler resolves table names in priority order:
|
||||||
|
|
||||||
|
1. `TableNameProvider` interface — `TableName() string` (can return `"schema.table"`)
|
||||||
|
2. `SchemaProvider` interface — `SchemaName() string` (combined with entity name)
|
||||||
|
3. Fallback: `schema.entity` (or `schema_entity` for SQLite)
|
||||||
@@ -46,7 +46,7 @@ func getCursorFilter(
|
|||||||
reverse := direction < 0
|
reverse := direction < 0
|
||||||
|
|
||||||
for _, s := range sortItems {
|
for _, s := range sortItems {
|
||||||
col := strings.TrimSpace(s.Column)
|
col := strings.Trim(strings.TrimSpace(s.Column), "()")
|
||||||
if col == "" {
|
if col == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/mark3labs/mcp-go/server"
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
|
||||||
@@ -21,17 +23,19 @@ type Handler struct {
|
|||||||
registry common.ModelRegistry
|
registry common.ModelRegistry
|
||||||
hooks *HookRegistry
|
hooks *HookRegistry
|
||||||
mcpServer *server.MCPServer
|
mcpServer *server.MCPServer
|
||||||
|
config Config
|
||||||
name string
|
name string
|
||||||
version string
|
version string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHandler creates a Handler with the given database and model registry.
|
// NewHandler creates a Handler with the given database, model registry, and config.
|
||||||
func NewHandler(db common.Database, registry common.ModelRegistry) *Handler {
|
func NewHandler(db common.Database, registry common.ModelRegistry, cfg Config) *Handler {
|
||||||
return &Handler{
|
return &Handler{
|
||||||
db: db,
|
db: db,
|
||||||
registry: registry,
|
registry: registry,
|
||||||
hooks: NewHookRegistry(),
|
hooks: NewHookRegistry(),
|
||||||
mcpServer: server.NewMCPServer("resolvemcp", "1.0.0"),
|
mcpServer: server.NewMCPServer("resolvemcp", "1.0.0"),
|
||||||
|
config: cfg,
|
||||||
name: "resolvemcp",
|
name: "resolvemcp",
|
||||||
version: "1.0.0",
|
version: "1.0.0",
|
||||||
}
|
}
|
||||||
@@ -52,6 +56,63 @@ func (h *Handler) MCPServer() *server.MCPServer {
|
|||||||
return h.mcpServer
|
return h.mcpServer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SSEServer returns an http.Handler that serves MCP over SSE.
|
||||||
|
// Config.BasePath must be set. Config.BaseURL is used when set; if empty it is
|
||||||
|
// detected automatically from each incoming request.
|
||||||
|
func (h *Handler) SSEServer() http.Handler {
|
||||||
|
if h.config.BaseURL != "" {
|
||||||
|
return h.newSSEServer(h.config.BaseURL, h.config.BasePath)
|
||||||
|
}
|
||||||
|
return &dynamicSSEHandler{h: h}
|
||||||
|
}
|
||||||
|
|
||||||
|
// newSSEServer creates a concrete *server.SSEServer for known baseURL and basePath values.
|
||||||
|
func (h *Handler) newSSEServer(baseURL, basePath string) *server.SSEServer {
|
||||||
|
return server.NewSSEServer(
|
||||||
|
h.mcpServer,
|
||||||
|
server.WithBaseURL(baseURL),
|
||||||
|
server.WithBasePath(basePath),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// dynamicSSEHandler detects BaseURL from each request and delegates to a cached
|
||||||
|
// *server.SSEServer per detected baseURL. Used when Config.BaseURL is empty.
|
||||||
|
type dynamicSSEHandler struct {
|
||||||
|
h *Handler
|
||||||
|
mu sync.Mutex
|
||||||
|
pool map[string]*server.SSEServer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *dynamicSSEHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
baseURL := requestBaseURL(r)
|
||||||
|
|
||||||
|
d.mu.Lock()
|
||||||
|
if d.pool == nil {
|
||||||
|
d.pool = make(map[string]*server.SSEServer)
|
||||||
|
}
|
||||||
|
s, ok := d.pool[baseURL]
|
||||||
|
if !ok {
|
||||||
|
s = d.h.newSSEServer(baseURL, d.h.config.BasePath)
|
||||||
|
d.pool[baseURL] = s
|
||||||
|
}
|
||||||
|
d.mu.Unlock()
|
||||||
|
|
||||||
|
s.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// requestBaseURL builds the base URL from an incoming request.
|
||||||
|
// It honours the X-Forwarded-Proto header for deployments behind a proxy.
|
||||||
|
func requestBaseURL(r *http.Request) string {
|
||||||
|
scheme := "http"
|
||||||
|
if r.TLS != nil {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
if proto := r.Header.Get("X-Forwarded-Proto"); proto != "" {
|
||||||
|
scheme = proto
|
||||||
|
}
|
||||||
|
return scheme + "://" + r.Host
|
||||||
|
}
|
||||||
|
|
||||||
// RegisterModel registers a model and immediately exposes it as MCP tools and a resource.
|
// RegisterModel registers a model and immediately exposes it as MCP tools and a resource.
|
||||||
func (h *Handler) RegisterModel(schema, entity string, model interface{}) error {
|
func (h *Handler) RegisterModel(schema, entity string, model interface{}) error {
|
||||||
fullName := buildModelName(schema, entity)
|
fullName := buildModelName(schema, entity)
|
||||||
|
|||||||
@@ -8,19 +8,19 @@
|
|||||||
//
|
//
|
||||||
// Usage:
|
// Usage:
|
||||||
//
|
//
|
||||||
// handler := resolvemcp.NewHandlerWithGORM(db)
|
// handler := resolvemcp.NewHandlerWithGORM(db, resolvemcp.Config{BaseURL: "http://localhost:8080"})
|
||||||
// handler.RegisterModel("public", "users", &User{})
|
// handler.RegisterModel("public", "users", &User{})
|
||||||
//
|
//
|
||||||
// r := mux.NewRouter()
|
// r := mux.NewRouter()
|
||||||
// resolvemcp.SetupMuxRoutes(r, handler, "http://localhost:8080")
|
// resolvemcp.SetupMuxRoutes(r, handler)
|
||||||
package resolvemcp
|
package resolvemcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/mark3labs/mcp-go/server"
|
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
|
bunrouter "github.com/uptrace/bunrouter"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"github.com/bitechdev/ResolveSpec/pkg/common"
|
"github.com/bitechdev/ResolveSpec/pkg/common"
|
||||||
@@ -28,56 +28,73 @@ import (
|
|||||||
"github.com/bitechdev/ResolveSpec/pkg/modelregistry"
|
"github.com/bitechdev/ResolveSpec/pkg/modelregistry"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config holds configuration for the resolvemcp handler.
|
||||||
|
type Config struct {
|
||||||
|
// BaseURL is the public-facing base URL of the server (e.g. "http://localhost:8080").
|
||||||
|
// It is sent to MCP clients during the SSE handshake so they know where to POST messages.
|
||||||
|
BaseURL string
|
||||||
|
|
||||||
|
// BasePath is the URL path prefix where the MCP endpoints are mounted (e.g. "/mcp").
|
||||||
|
// If empty, the path is detected from each incoming request automatically.
|
||||||
|
BasePath string
|
||||||
|
}
|
||||||
|
|
||||||
// NewHandlerWithGORM creates a Handler backed by a GORM database connection.
|
// NewHandlerWithGORM creates a Handler backed by a GORM database connection.
|
||||||
func NewHandlerWithGORM(db *gorm.DB) *Handler {
|
func NewHandlerWithGORM(db *gorm.DB, cfg Config) *Handler {
|
||||||
return NewHandler(database.NewGormAdapter(db), modelregistry.NewModelRegistry())
|
return NewHandler(database.NewGormAdapter(db), modelregistry.NewModelRegistry(), cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHandlerWithBun creates a Handler backed by a Bun database connection.
|
// NewHandlerWithBun creates a Handler backed by a Bun database connection.
|
||||||
func NewHandlerWithBun(db *bun.DB) *Handler {
|
func NewHandlerWithBun(db *bun.DB, cfg Config) *Handler {
|
||||||
return NewHandler(database.NewBunAdapter(db), modelregistry.NewModelRegistry())
|
return NewHandler(database.NewBunAdapter(db), modelregistry.NewModelRegistry(), cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHandlerWithDB creates a Handler using an existing common.Database and a new registry.
|
// NewHandlerWithDB creates a Handler using an existing common.Database and a new registry.
|
||||||
func NewHandlerWithDB(db common.Database) *Handler {
|
func NewHandlerWithDB(db common.Database, cfg Config) *Handler {
|
||||||
return NewHandler(db, modelregistry.NewModelRegistry())
|
return NewHandler(db, modelregistry.NewModelRegistry(), cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupMuxRoutes mounts the MCP HTTP/SSE endpoints on the given Gorilla Mux router.
|
// SetupMuxRoutes mounts the MCP HTTP/SSE endpoints on the given Gorilla Mux router
|
||||||
//
|
// using the base path from Config.BasePath (falls back to "/mcp" if empty).
|
||||||
// baseURL is the public-facing base URL of the server (e.g. "http://localhost:8080").
|
|
||||||
// It is sent to MCP clients during the SSE handshake so they know where to POST messages.
|
|
||||||
//
|
//
|
||||||
// Two routes are registered:
|
// Two routes are registered:
|
||||||
// - GET /mcp/sse — SSE connection endpoint (client subscribes here)
|
// - GET {basePath}/sse — SSE connection endpoint (client subscribes here)
|
||||||
// - POST /mcp/message — JSON-RPC message endpoint (client sends requests here)
|
// - POST {basePath}/message — JSON-RPC message endpoint (client sends requests here)
|
||||||
//
|
//
|
||||||
// To protect these routes with authentication, wrap the mux router or apply middleware
|
// To protect these routes with authentication, wrap the mux router or apply middleware
|
||||||
// before calling SetupMuxRoutes.
|
// before calling SetupMuxRoutes.
|
||||||
func SetupMuxRoutes(muxRouter *mux.Router, handler *Handler, baseURL string) {
|
func SetupMuxRoutes(muxRouter *mux.Router, handler *Handler) {
|
||||||
sseServer := server.NewSSEServer(
|
basePath := handler.config.BasePath
|
||||||
handler.mcpServer,
|
h := handler.SSEServer()
|
||||||
server.WithBaseURL(baseURL),
|
|
||||||
server.WithBasePath("/mcp"),
|
|
||||||
)
|
|
||||||
|
|
||||||
muxRouter.Handle("/mcp/sse", sseServer.SSEHandler()).Methods("GET", "OPTIONS")
|
muxRouter.Handle(basePath+"/sse", h).Methods("GET", "OPTIONS")
|
||||||
muxRouter.Handle("/mcp/message", sseServer.MessageHandler()).Methods("POST", "OPTIONS")
|
muxRouter.Handle(basePath+"/message", h).Methods("POST", "OPTIONS")
|
||||||
|
|
||||||
// Convenience: also expose the full SSE server at /mcp for clients that
|
// Convenience: also expose the full SSE server at basePath for clients that
|
||||||
// use ServeHTTP directly (e.g. net/http default mux).
|
// use ServeHTTP directly (e.g. net/http default mux).
|
||||||
muxRouter.PathPrefix("/mcp").Handler(http.StripPrefix("/mcp", sseServer))
|
muxRouter.PathPrefix(basePath).Handler(http.StripPrefix(basePath, h))
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSSEServer creates an *server.SSEServer that can be mounted manually,
|
// SetupBunRouterRoutes mounts the MCP HTTP/SSE endpoints on a bunrouter router
|
||||||
// useful when integrating with non-Mux routers or adding extra middleware.
|
// using the base path from Config.BasePath.
|
||||||
//
|
//
|
||||||
// sseServer := resolvemcp.NewSSEServer(handler, "http://localhost:8080", "/mcp")
|
// Two routes are registered:
|
||||||
// http.Handle("/mcp/", http.StripPrefix("/mcp", sseServer))
|
// - GET {basePath}/sse — SSE connection endpoint
|
||||||
func NewSSEServer(handler *Handler, baseURL, basePath string) *server.SSEServer {
|
// - POST {basePath}/message — JSON-RPC message endpoint
|
||||||
return server.NewSSEServer(
|
func SetupBunRouterRoutes(router *bunrouter.Router, handler *Handler) {
|
||||||
handler.mcpServer,
|
basePath := handler.config.BasePath
|
||||||
server.WithBaseURL(baseURL),
|
h := handler.SSEServer()
|
||||||
server.WithBasePath(basePath),
|
|
||||||
)
|
router.GET(basePath+"/sse", bunrouter.HTTPHandler(h))
|
||||||
|
router.POST(basePath+"/message", bunrouter.HTTPHandler(h))
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSSEServer returns an http.Handler that serves MCP over SSE.
|
||||||
|
// If Config.BasePath is set it is used directly; otherwise the base path is
|
||||||
|
// detected from each incoming request (by stripping the "/sse" or "/message" suffix).
|
||||||
|
//
|
||||||
|
// h := resolvemcp.NewSSEServer(handler)
|
||||||
|
// http.Handle("/api/mcp/", h)
|
||||||
|
func NewSSEServer(handler *Handler) http.Handler {
|
||||||
|
return handler.SSEServer()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ func GetCursorFilter(
|
|||||||
// 4. Process each sort column
|
// 4. Process each sort column
|
||||||
// --------------------------------------------------------------------- //
|
// --------------------------------------------------------------------- //
|
||||||
for _, s := range sortItems {
|
for _, s := range sortItems {
|
||||||
col := strings.TrimSpace(s.Column)
|
col := strings.Trim(strings.TrimSpace(s.Column), "()")
|
||||||
if col == "" {
|
if col == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func (opts *ExtendedRequestOptions) GetCursorFilter(
|
|||||||
// 4. Process each sort column
|
// 4. Process each sort column
|
||||||
// --------------------------------------------------------------------- //
|
// --------------------------------------------------------------------- //
|
||||||
for _, s := range sortItems {
|
for _, s := range sortItems {
|
||||||
col := strings.TrimSpace(s.Column)
|
col := strings.Trim(strings.TrimSpace(s.Column), "()")
|
||||||
if col == "" {
|
if col == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user