mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-05-25 16:00:35 +00:00
72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
func main() {
|
|
server := mcp.NewServer(&mcp.Implementation{
|
|
Name: "picoclaw-integration-streamable-server",
|
|
Version: "1.0.0",
|
|
}, nil)
|
|
|
|
mcp.AddTool(server, &mcp.Tool{
|
|
Name: "echo",
|
|
Description: "Echo back the provided message",
|
|
}, func(ctx context.Context, req *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
|
|
message, _ := args["message"].(string)
|
|
return &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
&mcp.TextContent{Text: message},
|
|
},
|
|
}, nil, nil
|
|
})
|
|
|
|
streamable := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server {
|
|
return server
|
|
}, &mcp.StreamableHTTPOptions{
|
|
JSONResponse: envBool("STREAMABLE_JSON_RESPONSE", true),
|
|
})
|
|
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/mcp", streamable)
|
|
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte("ok"))
|
|
})
|
|
|
|
srv := &http.Server{
|
|
Addr: ":8080",
|
|
Handler: mux,
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
}
|
|
|
|
log.Printf("streamable MCP integration server listening on %s", srv.Addr)
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func envBool(name string, fallback bool) bool {
|
|
value := strings.TrimSpace(strings.ToLower(os.Getenv(name)))
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
|
|
switch value {
|
|
case "1", "true", "yes", "on":
|
|
return true
|
|
case "0", "false", "no", "off":
|
|
return false
|
|
default:
|
|
return fallback
|
|
}
|
|
}
|