Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8748fe53bc | |||
| 8d8d8fac65 | |||
| 3dd01e5313 | |||
| d2c2962ad1 | |||
| c5375b8a77 |
@@ -939,7 +939,7 @@ local function _check_auth(self, conn)
|
|||||||
if not self._auth_token then return true end
|
if not self._auth_token then return true end
|
||||||
if conn.method == "OPTIONS" then return true end
|
if conn.method == "OPTIONS" then return true end
|
||||||
local auth = conn.headers["authorization"] or ""
|
local auth = conn.headers["authorization"] or ""
|
||||||
local token = auth:match("^Bearer%s+(.+)$")
|
local token = auth:match("^[Bb]earer%s+(.+)$")
|
||||||
return token == self._auth_token
|
return token == self._auth_token
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -200,6 +200,13 @@ end
|
|||||||
local server_name = os.getenv("LMCP_NAME") or (WINDOWS and "windows-tools" or "linux-tools")
|
local server_name = os.getenv("LMCP_NAME") or (WINDOWS and "windows-tools" or "linux-tools")
|
||||||
local server = lmcp.new(server_name, {
|
local server = lmcp.new(server_name, {
|
||||||
port = tonumber(os.getenv("LMCP_PORT") or arg[1]) or 8080,
|
port = tonumber(os.getenv("LMCP_PORT") or arg[1]) or 8080,
|
||||||
|
-- LMCP_HOST: bind interface (default 0.0.0.0). Hosts that need
|
||||||
|
-- single-interface binding (hertz: 192.168.88.18 only) set this.
|
||||||
|
host = os.getenv("LMCP_HOST"),
|
||||||
|
-- LMCP_CONF: path to a conf file with bearer-token entries
|
||||||
|
-- (e.g. /opt/herding/etc/hertz-tools.conf). Read by lmcp.lua's
|
||||||
|
-- read_conf; the `.godparticle` entry becomes the bearer token.
|
||||||
|
conf = os.getenv("LMCP_CONF"),
|
||||||
})
|
})
|
||||||
|
|
||||||
-- ---- Tools ----
|
-- ---- Tools ----
|
||||||
|
|||||||
@@ -0,0 +1,103 @@
|
|||||||
|
-- /opt/lmcp/tools.d/nash.lua -- nash shared memory tools
|
||||||
|
--
|
||||||
|
-- Provides add/search/list/delete tools for the nash memory service.
|
||||||
|
-- Requires NASH_URL env var (e.g. http://192.168.88.143:8000).
|
||||||
|
-- When unset, tools return an error guiding the user to set it.
|
||||||
|
--
|
||||||
|
-- SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
local server, run = ...
|
||||||
|
|
||||||
|
local function nash_url()
|
||||||
|
local url = os.getenv("NASH_URL")
|
||||||
|
if not url or url == "" then return nil end
|
||||||
|
return url:gsub("/+$", "")
|
||||||
|
end
|
||||||
|
|
||||||
|
local function curl_json(url, method, body, timeout)
|
||||||
|
timeout = timeout or 10
|
||||||
|
local out = "/tmp/lmcp-nash-" .. os.time() .. "-" .. math.random(10000, 99999) .. ".json"
|
||||||
|
local fmt = "http_code=%{http_code}"
|
||||||
|
local cmd
|
||||||
|
if body then
|
||||||
|
local tmp = out .. ".req"
|
||||||
|
local f = io.open(tmp, "w")
|
||||||
|
if f then f:write(body); f:close() end
|
||||||
|
cmd = string.format(
|
||||||
|
"curl -sS -X %s --max-time %d -H 'Content-Type: application/json' --data-binary '@%s' -o '%s' -w '%s' '%s'",
|
||||||
|
method, timeout, tmp, out, fmt, url
|
||||||
|
)
|
||||||
|
os.execute("rm -f '" .. tmp .. "' 2>/dev/null")
|
||||||
|
else
|
||||||
|
cmd = string.format(
|
||||||
|
"curl -sS -X %s --max-time %d -o '%s' -w '%s' '%s'",
|
||||||
|
method, timeout, out, fmt, url
|
||||||
|
)
|
||||||
|
end
|
||||||
|
local raw = run(cmd, timeout + 5) or ""
|
||||||
|
local http_code = tonumber(raw:match("(%d+)$")) or 0
|
||||||
|
local body_out = ""
|
||||||
|
local f = io.open(out, "r")
|
||||||
|
if f then body_out = f:read("*a") or ""; f:close() end
|
||||||
|
os.execute("rm -f '" .. out .. "' 2>/dev/null")
|
||||||
|
return body_out, http_code
|
||||||
|
end
|
||||||
|
|
||||||
|
server:tool("nash_add", "Store a text entry in shared nash memory.", {
|
||||||
|
type = "object",
|
||||||
|
properties = {
|
||||||
|
text = { type = "string", description = "Text content to remember" },
|
||||||
|
},
|
||||||
|
required = { "text" },
|
||||||
|
}, function(a)
|
||||||
|
local base = nash_url()
|
||||||
|
if not base then return "Error: set NASH_URL env var (e.g. http://192.168.88.143:8000)" end
|
||||||
|
local json, code = curl_json(base .. "/add", "POST", '{"text":' .. require("json").encode(a.text) .. '}')
|
||||||
|
if code ~= 200 then return "Error: nash API HTTP " .. code .. ": " .. json end
|
||||||
|
return json
|
||||||
|
end)
|
||||||
|
|
||||||
|
server:tool("nash_search", "Semantic search across shared nash memory.", {
|
||||||
|
type = "object",
|
||||||
|
properties = {
|
||||||
|
query = { type = "string", description = "Search query" },
|
||||||
|
limit = { type = "integer", description = "Max results (default 5)", default = 5 },
|
||||||
|
},
|
||||||
|
required = { "query" },
|
||||||
|
}, function(a)
|
||||||
|
local base = nash_url()
|
||||||
|
if not base then return "Error: set NASH_URL env var (e.g. http://192.168.88.143:8000)" end
|
||||||
|
local body = '{"query":' .. require("json").encode(a.query) .. ',"limit":' .. (a.limit or 5) .. '}'
|
||||||
|
local json, code = curl_json(base .. "/search", "POST", body)
|
||||||
|
if code ~= 200 then return "Error: nash API HTTP " .. code .. ": " .. json end
|
||||||
|
return json
|
||||||
|
end)
|
||||||
|
|
||||||
|
server:tool("nash_list", "List all entries in shared nash memory.", {
|
||||||
|
type = "object",
|
||||||
|
properties = {
|
||||||
|
limit = { type = "integer", description = "Max results (default 100)", default = 100 },
|
||||||
|
},
|
||||||
|
}, function(a)
|
||||||
|
local base = nash_url()
|
||||||
|
if not base then return "Error: set NASH_URL env var (e.g. http://192.168.88.143:8000)" end
|
||||||
|
local body = '{"limit":' .. (a.limit or 100) .. '}'
|
||||||
|
local json, code = curl_json(base .. "/scroll", "POST", body)
|
||||||
|
if code ~= 200 then return "Error: nash API HTTP " .. code .. ": " .. json end
|
||||||
|
return json
|
||||||
|
end)
|
||||||
|
|
||||||
|
server:tool("nash_delete", "Delete an entry from shared nash memory by ID.", {
|
||||||
|
type = "object",
|
||||||
|
properties = {
|
||||||
|
id = { type = "string", description = "Entry ID to delete" },
|
||||||
|
},
|
||||||
|
required = { "id" },
|
||||||
|
}, function(a)
|
||||||
|
local base = nash_url()
|
||||||
|
if not base then return "Error: set NASH_URL env var (e.g. http://192.168.88.143:8000)" end
|
||||||
|
local body = '{"id":' .. require("json").encode(a.id) .. '}'
|
||||||
|
local json, code = curl_json(base .. "/delete", "POST", body)
|
||||||
|
if code ~= 200 then return "Error: nash API HTTP " .. code .. ": " .. json end
|
||||||
|
return json
|
||||||
|
end)
|
||||||
Reference in New Issue
Block a user