Cortex

by FreePeak

25 stars
341 downloads
Not rated
GitHub

About

A declarative platform for building Model Context Protocol (MCP) servers in Golang—exposing tools, resources & prompts in a clean, structured way

Details

Author
FreePeak
GitHub stars
25
Downloads
341
Categories
Other

- Declarative MCP server construction in Go
- Supports Resources, Tools, Prompts, and Providers
- STDIO and HTTP/SSE transport options
- Embeddable into existing Go applications and HTTP servers
- Multi-protocol operation via goroutines
- Adheres to the latest MCP specification

Install with go get github.com/FreePeak/cortex. Define tools using the tools package, create a server with server.NewMCPServer, register tools with handlers, and start the server with ServeStdio() or ServeHTTP(). Providers can group related tools and resources for registration.

Embedding Cortex

Cortex can be embedded into existing applications to add MCP capabilities without running a separate server. This is useful for integrating with existing web frameworks or applications like PocketBase.

HTTP Server Integration

You can easily integrate Cortex with any Go HTTP server:

package main

import (
"log"
"net/http"
"os"

"github.com/FreePeak/cortex/pkg/server"
"github.com/FreePeak/cortex/pkg/tools"
)

func main() {
// Create a logger
logger := log.New(os.Stderr, "[cortex] ", log.LstdFlags)

// Create an MCP server
mcpServer := server.NewMCPServer("Embedded MCP Server", "1.0.0", logger)

// Add some tools
echoTool := tools.NewTool("echo",
tools.WithDescription("Echoes back the input message"),
tools.WithString("message",
tools.Description("The message to echo back"),
tools.Required(),
),
)

// Add the tool to the server
mcpServer.AddTool(context.Background(), echoTool, func(ctx context.Context, request server.ToolCallRequest) (interface{}, error) {
message := request.Parameters["message"].(string)
return map[string]interface{}{
"content": []map[string]interface{}{
{
"type": "text",
"text": message,
},
},
}, nil
})

// Create an HTTP adapter for the MCP server
adapter := server.NewHTTPAdapter(mcpServer, server.WithPath("/api/mcp"))

// Use the adapter in your HTTP server
http.Handle("/api/mcp/", adapter.Handler())

// Add your other routes
http.HandleFunc("/", func(w http.ResponseWriter, r http.Request) {
w.Write([]byte("Hello from the main server!"))
})

// Start the server
logger.Println("Starting server on :8080")
http.ListenAndServe(":8080", nil)
}

PocketBase Integration

Cortex can be integrated with PocketBase, an open-source backend with database, auth, and admin UI:

package main

import (
"log"

"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"

"github.com/FreePeak/cortex/pkg/integration/pocketbase"
"github.com/FreePeak/cortex/pkg/tools"
)

func main() {
// Create a new PocketBase app
app := pocketbase.New()

// Initialize Cortex plugin
plugin := pocketbase.NewCortexPlugin(
pocketbase.WithName("PocketBase MCP Server"),
pocketbase.WithVersion("1.0.0"),
pocketbase.WithBasePath("/api/mcp"),
)

// Add tools to the plugin
echoTool := tools.NewTool("echo",
tools.WithDescription("Echoes back the input message"),
tools.WithString("message",
tools.Description("The message to echo back"),
tools.Required(),
),
)

// Add the tool with a handler
plugin.AddTool(echoTool, func(ctx context.Context, request pocketbase.ToolCallRequest) (interface{}, error) {
message := request.Parameters["message"].(string)
return map[string]interface{}{
"content": []map[string]interface{}{
{
"type": "text",
"text": message,
},
},
}, nil
})

// Register the plugin with PocketBase
app.OnBeforeServe().Add(func(e
core.ServeEvent) error {
// Register the plugin
return plugin.RegisterWithPocketBase(app)
})

// Start the PocketBase app
if err := app.Start(); err != nil {
log.Fatal(err)
}
}

For more detailed documentation on embedding Cortex, see the Embedding Guide.

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.