Skip to content

Commit 1a4dab0

Browse files
mcp-reverse-proxy (#575)
* mcp-reverse-proxy Summary: - MCP server pass through to `postgres` connection. - Added robot test `Concurrent psql and Reverse Proxy MCP HTTP Server Query Tool`. - Uplifted wirre protocol server. * - Linter issue.
1 parent 2d08cb5 commit 1a4dab0

File tree

10 files changed

+459
-42
lines changed

10 files changed

+459
-42
lines changed

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,30 @@
512512
"--pgsrv.debug.enable=${input:serverDebugPublish}",
513513
],
514514
},
515+
{
516+
"name": "run server AND mcp",
517+
"type": "go",
518+
"request": "launch",
519+
"envFile": "${workspaceFolder}/.vscode/.env",
520+
"mode": "debug",
521+
"program": "${workspaceFolder}/stackql",
522+
"args": [
523+
"srv",
524+
"--pgsrv.port=5885",
525+
"--tls.allowInsecure",
526+
"--auth=${input:authString}",
527+
"--session=${input:sessionString}",
528+
"--gc=${input:gcString}",
529+
"--registry=${input:registryString}",
530+
"--namespaces=${input:namespaceString}",
531+
"--sqlBackend=${input:sqlBackendString}",
532+
"--dbInternal=${input:dbInternalString}",
533+
"--export.alias=${input:exportAliasString}",
534+
"--pgsrv.debug.enable=${input:serverDebugPublish}",
535+
"--mcp.server.type=reverse_proxy",
536+
"--mcp.config={\"server\": {\"transport\": \"http\", \"address\": \"127.0.0.1:9914\"}, \"backend\": {\"dsn\": \"postgres://stackql:stackql@127.0.0.1:5885?default_query_exec_mode=simple_protocol&standard_conforming_strings=on&client_encoding=UTF8\"}}"
537+
],
538+
},
515539
{
516540
"name": "run mTLS server",
517541
"type": "go",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/spf13/viper v1.10.1
2020
github.com/stackql/any-sdk v0.2.2-beta07
2121
github.com/stackql/go-suffix-map v0.0.1-alpha01
22-
github.com/stackql/psql-wire v0.1.1-beta25
22+
github.com/stackql/psql-wire v0.1.2-alpha01
2323
github.com/stackql/stackql-parser v0.0.15-alpha06
2424
github.com/stretchr/testify v1.10.0
2525
golang.org/x/sync v0.15.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,8 @@ github.com/stackql/any-sdk v0.2.2-beta07 h1:c/MaT8p4lB30xslJo9LQm3JDWMMfzwheGXqf
469469
github.com/stackql/any-sdk v0.2.2-beta07/go.mod h1:m1o5TCfyKkdt2bREB3itwPv1MhM+lk4eu24KpPohFoY=
470470
github.com/stackql/go-suffix-map v0.0.1-alpha01 h1:TDUDS8bySu41Oo9p0eniUeCm43mnRM6zFEd6j6VUaz8=
471471
github.com/stackql/go-suffix-map v0.0.1-alpha01/go.mod h1:QAi+SKukOyf4dBtWy8UMy+hsXXV+yyEE4vmBkji2V7g=
472-
github.com/stackql/psql-wire v0.1.1-beta25 h1:DFBLjtz9N1S9gIYhqsjVZtVZMVSg7c0vvirPT29+S3s=
473-
github.com/stackql/psql-wire v0.1.1-beta25/go.mod h1:a44Wd8kDC3irFLpGutarKDBqhJ/aqXlj1aMzO5bVJYg=
472+
github.com/stackql/psql-wire v0.1.2-alpha01 h1:RMBRURGspmSNqm2/sgoEc+D6Sri2y/3drjl4nKlOOi4=
473+
github.com/stackql/psql-wire v0.1.2-alpha01/go.mod h1:a44Wd8kDC3irFLpGutarKDBqhJ/aqXlj1aMzO5bVJYg=
474474
github.com/stackql/readline v0.0.2-alpha05 h1:ID4QzGdplFBsrSnTuz8pvKzWw96JbrJg8fsLry2UriU=
475475
github.com/stackql/readline v0.0.2-alpha05/go.mod h1:OFAYOdXk/X4+5GYiDXFfaGrk+bCN6Qv0SYY5HNzD2E0=
476476
github.com/stackql/stackql-go-sqlite3 v1.0.4-stackql h1:fp70Vdw+PCVEoPrAhkyqPuAlrIiHT79mght/0rlR4oY=

internal/stackql/acid/tsm_physio/best_effort_coordinator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tsm_physio //nolint:stylecheck // prefer this nomenclature
1+
package tsm_physio //nolint:stylecheck,revive // prefer this nomenclature
22

33
import (
44
"fmt"

internal/stackql/cmd/mcp.go

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ import (
2121

2222
"github.com/spf13/cobra"
2323

24+
"github.com/stackql/any-sdk/pkg/db/db_util"
25+
"github.com/stackql/any-sdk/pkg/dto"
2426
"github.com/stackql/any-sdk/pkg/logging"
2527
"github.com/stackql/stackql/internal/stackql/acid/tsm_physio"
2628
"github.com/stackql/stackql/internal/stackql/entryutil"
2729
"github.com/stackql/stackql/internal/stackql/handler"
2830
"github.com/stackql/stackql/internal/stackql/iqlerror"
2931
"github.com/stackql/stackql/internal/stackql/mcpbackend"
3032
"github.com/stackql/stackql/pkg/mcp_server"
33+
34+
_ "github.com/jackc/pgx/v5" //nolint:revive // canonical driver pattern
3135
)
3236

3337
//nolint:gochecknoglobals // cobra pattern
@@ -68,27 +72,45 @@ func runMCPServer(handlerCtx handler.HandlerContext) {
6872
if config.Server.IsReadOnly != nil {
6973
isReadOnly = *config.Server.IsReadOnly
7074
}
71-
orchestrator, orchestratorErr := tsm_physio.NewOrchestrator(handlerCtx)
72-
iqlerror.PrintErrorAndExitOneIfError(orchestratorErr)
73-
iqlerror.PrintErrorAndExitOneIfNil(orchestrator, "orchestrator is unexpectedly nil")
74-
// handlerCtx.SetTSMOrchestrator(orchestrator)
75-
backend, backendErr := mcpbackend.NewStackqlMCPBackendService(
76-
isReadOnly,
77-
orchestrator,
78-
handlerCtx,
79-
logging.GetLogger(),
80-
)
81-
iqlerror.PrintErrorAndExitOneIfError(backendErr)
82-
iqlerror.PrintErrorAndExitOneIfNil(backend, "mcp backend is unexpectedly nil")
83-
75+
var backend mcp_server.Backend
76+
var backendErr error
77+
if mcpServerType == "reverse_proxy" {
78+
config.Server.Transport = "http"
79+
dsn := config.GetBackendConnectionString()
80+
// conn
81+
var cfg dto.SQLBackendCfg
82+
cfg.DSN = dsn
83+
cfg.InitMaxRetries = 5
84+
cfg.InitRetryInitialDelay = 2
85+
db, err := db_util.GetDB("pgx", "postgres", cfg)
86+
iqlerror.PrintErrorAndExitOneIfError(err)
87+
backend, backendErr = mcpbackend.NewStackqlMCPReverseProxyService(
88+
isReadOnly,
89+
dsn,
90+
db,
91+
handlerCtx,
92+
logging.GetLogger(),
93+
)
94+
iqlerror.PrintErrorAndExitOneIfError(backendErr)
95+
} else {
96+
orchestrator, orchestratorErr := tsm_physio.NewOrchestrator(handlerCtx)
97+
iqlerror.PrintErrorAndExitOneIfError(orchestratorErr)
98+
iqlerror.PrintErrorAndExitOneIfNil(orchestrator, "orchestrator is unexpectedly nil")
99+
// handlerCtx.SetTSMOrchestrator(orchestrator)
100+
backend, backendErr = mcpbackend.NewStackqlMCPBackendService(
101+
isReadOnly,
102+
orchestrator,
103+
handlerCtx,
104+
logging.GetLogger(),
105+
)
106+
iqlerror.PrintErrorAndExitOneIfError(backendErr)
107+
iqlerror.PrintErrorAndExitOneIfNil(backend, "mcp backend is unexpectedly nil")
108+
}
84109
server, serverErr := mcp_server.NewAgnosticBackendServer(
85110
backend,
86111
&config,
87112
logging.GetLogger(),
88113
)
89-
// server, serverErr := mcp_server.NewExampleHTTPBackendServer(
90-
// logging.GetLogger(),
91-
// )
92114
iqlerror.PrintErrorAndExitOneIfError(serverErr)
93115
server.Start(context.Background()) //nolint:errcheck // TODO: investigate
94116
}

0 commit comments

Comments
 (0)