Cortex

SSE

by FreePeak

25 341 downloads Not rated yet Apache-2.0

About

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

Details

Transport
SSE
License
Apache-2.0

Explore

- 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

go get github.com/FreePeak/cortex

MCP servers in Go can be connected to different transports depending on your use case:

cortex_recall

Sub-65ms semantic and graph recall in long-term Cortex memory.

cortex_get_project_spec

Deterministic retrieval of Living Canonical Project Specification.

cortex_store_memory

Permanent storage of project specifications, architecture or facts.

cortex_triage_general

Assisted project compaction and migration from general incubator.

cortex_ingest_turn

Mandatory conversational turn buffer ingestion.

Tools let LLMs take actions through your server. Unlike resources, tools are expected to perform computation and have side effects:

// Define a calculator tool
calculatorTool := tools.NewTool("calculator",
    tools.WithDescription("Performs basic math operations"),
    tools.WithString("operation",
        tools.Description("The operation to perform (add, subtract, multiply, divide)"),
        tools.Required(),
    ),
    tools.WithNumber("a", 
        tools.Description("First operand"),
        tools.Required(),
    ),
    tools.WithNumber("b", 
        tools.Description("Second operand"),
        tools.Required(),
    ),
)

// Add the tool to the server with a handler
mcpServer.AddTool(ctx, calculatorTool, handleCalculator)

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.

Videos about Cortex

Relevant YouTube tutorials, setups, and demos