Files
membank/aio-mcp/internal/server/serve.go
张威33321 1a6ba86f04 0908
2026-09-08 20:36:25 +08:00

33 lines
969 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package server
import (
"context"
"fmt"
"log/slog"
"net/http"
"github.com/modelcontextprotocol/go-sdk/mcp"
"aio-mcp/internal/config"
)
// Serve 按传输方式启动服务器,阻塞直到 stdin 关闭、监听失败或出错。
func Serve(s *mcp.Server, cfg *config.Config) error {
switch cfg.Transport {
case config.TransportStdio:
slog.Info("aio-mcp starting", "transport", "stdio")
// server.Run 会阻塞,直到 stdin 关闭或出错。
return s.Run(context.Background(), &mcp.StdioTransport{})
case config.TransportHTTP:
handler := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server { return s }, nil)
slog.Info("aio-mcp starting", "transport", "http", "addr", cfg.Addr)
return http.ListenAndServe(cfg.Addr, handler)
default:
// config.Parse 已校验过,这里仅为防御性兜底。
return fmt.Errorf("未知 transport: %q支持: %s, %s",
cfg.Transport, config.TransportStdio, config.TransportHTTP)
}
}