Embeds a Model Context Protocol (MCP) server into the Memos HTTP process, exposing memo operations as MCP tools at POST/GET /mcp using Streamable HTTP transport. Authentication is PAT-only — requests without a valid personal access token receive HTTP 401. Six tools are exposed: list_memos, get_memo, create_memo, update_memo, delete_memo, and search_memos, all scoped to the authenticated user.
31 lines
791 B
Go
31 lines
791 B
Go
package mcp
|
|
|
|
import (
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/labstack/echo/v5/middleware"
|
|
mcpserver "github.com/mark3labs/mcp-go/server"
|
|
|
|
"github.com/usememos/memos/store"
|
|
)
|
|
|
|
type MCPService struct {
|
|
store *store.Store
|
|
}
|
|
|
|
func NewMCPService(store *store.Store) *MCPService {
|
|
return &MCPService{store: store}
|
|
}
|
|
|
|
func (s *MCPService) RegisterRoutes(echoServer *echo.Echo) {
|
|
mcpSrv := mcpserver.NewMCPServer("Memos", "1.0.0", mcpserver.WithToolCapabilities(false))
|
|
s.registerMemoTools(mcpSrv)
|
|
|
|
httpHandler := mcpserver.NewStreamableHTTPServer(mcpSrv)
|
|
|
|
mcpGroup := echoServer.Group("")
|
|
mcpGroup.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
|
AllowOrigins: []string{"*"},
|
|
}))
|
|
mcpGroup.Use(newAuthMiddleware(s.store))
|
|
mcpGroup.Any("/mcp", echo.WrapHandler(httpHandler))
|
|
}
|