Skip to content

Commit 6458a27

Browse files
mcp-v2
Summary: - MCP server and postgres server run concurrently.
1 parent 2dcee5b commit 6458a27

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

internal/stackql/cmd/mcp.go

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/stackql/any-sdk/pkg/logging"
2525
"github.com/stackql/stackql/internal/stackql/acid/tsm_physio"
2626
"github.com/stackql/stackql/internal/stackql/entryutil"
27+
"github.com/stackql/stackql/internal/stackql/handler"
2728
"github.com/stackql/stackql/internal/stackql/iqlerror"
2829
"github.com/stackql/stackql/internal/stackql/mcpbackend"
2930
"github.com/stackql/stackql/pkg/mcp_server"
@@ -52,35 +53,42 @@ var mcpSrvCmd = &cobra.Command{
5253
handlerCtx, err := entryutil.BuildHandlerContext(runtimeCtx, nil, queryCache, inputBundle, false)
5354
iqlerror.PrintErrorAndExitOneIfError(err)
5455
iqlerror.PrintErrorAndExitOneIfNil(handlerCtx, "handler context is unexpectedly nil")
55-
var config mcp_server.Config
56-
json.Unmarshal([]byte(mcpConfig), &config) //nolint:errcheck // TODO: investigate
57-
config.Server.Transport = mcpServerType
58-
var isReadOnly bool
59-
if config.Server.IsReadOnly != nil {
60-
isReadOnly = *config.Server.IsReadOnly
56+
if mcpServerType == "" {
57+
mcpServerType = "http"
6158
}
62-
orchestrator, orchestratorErr := tsm_physio.NewOrchestrator(handlerCtx)
63-
iqlerror.PrintErrorAndExitOneIfError(orchestratorErr)
64-
iqlerror.PrintErrorAndExitOneIfNil(orchestrator, "orchestrator is unexpectedly nil")
65-
// handlerCtx.SetTSMOrchestrator(orchestrator)
66-
backend, backendErr := mcpbackend.NewStackqlMCPBackendService(
67-
isReadOnly,
68-
orchestrator,
69-
handlerCtx,
70-
logging.GetLogger(),
71-
)
72-
iqlerror.PrintErrorAndExitOneIfError(backendErr)
73-
iqlerror.PrintErrorAndExitOneIfNil(backend, "mcp backend is unexpectedly nil")
74-
75-
server, serverErr := mcp_server.NewAgnosticBackendServer(
76-
backend,
77-
&config,
78-
logging.GetLogger(),
79-
)
80-
// server, serverErr := mcp_server.NewExampleHTTPBackendServer(
81-
// logging.GetLogger(),
82-
// )
83-
iqlerror.PrintErrorAndExitOneIfError(serverErr)
84-
server.Start(context.Background()) //nolint:errcheck // TODO: investigate
59+
runMCPServer(handlerCtx)
8560
},
8661
}
62+
63+
func runMCPServer(handlerCtx handler.HandlerContext) {
64+
var config mcp_server.Config
65+
json.Unmarshal([]byte(mcpConfig), &config) //nolint:errcheck // TODO: investigate
66+
config.Server.Transport = mcpServerType
67+
var isReadOnly bool
68+
if config.Server.IsReadOnly != nil {
69+
isReadOnly = *config.Server.IsReadOnly
70+
}
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+
84+
server, serverErr := mcp_server.NewAgnosticBackendServer(
85+
backend,
86+
&config,
87+
logging.GetLogger(),
88+
)
89+
// server, serverErr := mcp_server.NewExampleHTTPBackendServer(
90+
// logging.GetLogger(),
91+
// )
92+
iqlerror.PrintErrorAndExitOneIfError(serverErr)
93+
server.Start(context.Background()) //nolint:errcheck // TODO: investigate
94+
}

internal/stackql/cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ func init() {
199199
rootCmd.AddCommand(srvCmd)
200200
rootCmd.AddCommand(mcpSrvCmd)
201201

202-
mcpSrvCmd.PersistentFlags().StringVar(&mcpConfig, "mcp.config", "{}", "MCP server config file path (YAML or JSON)")
203-
mcpSrvCmd.PersistentFlags().StringVar(&mcpServerType, "mcp.server.type", "http", "MCP server type (http or stdio for now)")
202+
rootCmd.PersistentFlags().StringVar(&mcpConfig, "mcp.config", "{}", "MCP server config file path (YAML or JSON)")
203+
rootCmd.PersistentFlags().StringVar(&mcpServerType, "mcp.server.type", "", "MCP server type (http or stdio for now)")
204204
}
205205

206206
func mergeConfigFromFile(runtimeCtx *dto.RuntimeCtx, flagSet pflag.FlagSet) {

internal/stackql/cmd/srv.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ var srvCmd = &cobra.Command{
4848
sbe := driver.NewStackQLDriverFactory(handlerCtx, runtimeCtx.PGSrvIsDebugNoticesEnabled)
4949
server, err := psqlwire.MakeWireServer(sbe, runtimeCtx)
5050
iqlerror.PrintErrorAndExitOneIfError(err)
51+
if mcpServerType != "" {
52+
go runMCPServer(handlerCtx.Clone()) //nolint:errcheck // TODO: investigate
53+
}
5154
server.Serve() //nolint:errcheck // TODO: investigate
5255
},
5356
}

0 commit comments

Comments
 (0)