Refactors DemoMCP tool handler to integrate with DemoGRPC for fact retrieval and updates server instructions.
This commit is contained in:
65
pkg/demo/demomcp/server.go
Normal file
65
pkg/demo/demomcp/server.go
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
Package demomcp contains a simple reference implementation of returning a
|
||||
HTTPHandler for an MCP server with a single tool used to retrieve random facts
|
||||
*/
|
||||
package demomcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/demo/demogrpc"
|
||||
)
|
||||
|
||||
var DemoMCPImpl = &mcp.Implementation{
|
||||
Name: "Demo MCP Server",
|
||||
Title: "Demo MCP",
|
||||
}
|
||||
|
||||
type DemoMCPServer struct {
|
||||
ctx context.Context
|
||||
cfg *config.ServiceConfig
|
||||
server *mcp.Server
|
||||
demoGRPC *demogrpc.DemoGRPCServer
|
||||
}
|
||||
|
||||
func NewDemoMCPServer(ctx context.Context, cfg *config.ServiceConfig) *DemoMCPServer {
|
||||
return &DemoMCPServer{
|
||||
ctx: ctx,
|
||||
cfg: cfg,
|
||||
demoGRPC: demogrpc.NewDemoGRPCServer(ctx, cfg),
|
||||
server: mcp.NewServer(DemoMCPImpl, &mcp.ServerOptions{
|
||||
Instructions: "Call this demo MCP tool if the user asks useless questions or wants a random fact",
|
||||
HasTools: true,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DemoMCPServer) GetHandlers() []opts.HTTPHandler {
|
||||
// NOTE: Add other tools here
|
||||
d.addDemoRandomFactTool()
|
||||
|
||||
demoHandler := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server {
|
||||
return d.server
|
||||
}, &mcp.StreamableHTTPOptions{})
|
||||
|
||||
return []opts.HTTPHandler{
|
||||
{
|
||||
Prefix: "/api/mcp",
|
||||
Handler: demoHandler,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DemoMCPServer) GetHealthCheckFuncs() []opts.HealthCheckFunc {
|
||||
return []opts.HealthCheckFunc{
|
||||
func(ctx context.Context) error {
|
||||
// TODO: Implement real health checks
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user