added authentication, $schema, fixed media adapter mcp

This commit is contained in:
dswbx
2025-08-07 11:33:46 +02:00
parent 1b02feca93
commit 42db5f55c7
9 changed files with 247 additions and 34 deletions

View File

@@ -5,6 +5,7 @@ import type { McpSchema } from "modules/mcp";
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { mcpSchemaSymbol } from "modules/mcp/McpSchemaHelper";
import { getVersion } from "cli/utils/sys";
export const mcp: CliCommand = (program) =>
program
@@ -12,15 +13,24 @@ export const mcp: CliCommand = (program) =>
.description("mcp server")
.option("--port <port>", "port to listen on", "3000")
.option("--path <path>", "path to listen on", "/mcp")
.option(
"--token <token>",
"token to authenticate requests, if not provided, uses BEARER_TOKEN environment variable",
)
.option("--log-level <level>", "log level")
.action(action);
async function action(options: { port: string; path: string }) {
async function action(options: {
port?: string;
path?: string;
token?: string;
logLevel?: string;
}) {
const app = await makeAppFromEnv({
server: "node",
});
//console.log(info(app.server));
const token = options.token || process.env.BEARER_TOKEN;
const middlewareServer = getMcpServer(app.server);
const appConfig = app.modules.configs();
@@ -33,25 +43,34 @@ async function action(options: { port: string; path: string }) {
) as s.Node<McpSchema>[];
const tools = [
...middlewareServer.tools,
...nodes.flatMap((n) => n.schema.getTools(n)),
...app.modules.ctx().mcp.tools,
...nodes.flatMap((n) => n.schema.getTools(n)),
];
const resources = [...middlewareServer.resources, ...app.modules.ctx().mcp.resources];
const server = new McpServer(
{
name: "bknd",
version: "0.0.1",
version: await getVersion(),
},
{ app, ctx: () => app.modules.ctx() },
tools,
resources,
);
if (token) {
server.setAuthentication({
type: "bearer",
token,
});
}
const hono = new Hono().use(
mcpMiddleware({
server,
sessionsEnabled: true,
debug: {
logLevel: options.logLevel as any,
explainEndpoint: true,
},
endpoint: {

View File

@@ -85,9 +85,6 @@ async function create(app: App, options: any) {
async function update(app: App, options: any) {
const config = app.module.auth.toJSON(true);
const strategy = app.module.auth.authenticator.strategy("password") as PasswordStrategy;
const users_entity = config.entity_name as "users";
const em = app.modules.ctx().em;
const email = (await $text({
message: "Which user? Enter email",
@@ -100,7 +97,10 @@ async function update(app: App, options: any) {
})) as string;
if ($isCancel(email)) process.exit(1);
const { data: user } = await em.repository(users_entity).findOne({ email });
const { data: user } = await app.modules
.ctx()
.em.repository(config.entity_name as "users")
.findOne({ email });
if (!user) {
$log.error("User not found");
process.exit(1);
@@ -118,26 +118,10 @@ async function update(app: App, options: any) {
});
if ($isCancel(password)) process.exit(1);
try {
function togglePw(visible: boolean) {
const field = em.entity(users_entity).field("strategy_value")!;
field.config.hidden = !visible;
field.config.fillable = visible;
}
togglePw(true);
await app.modules
.ctx()
.em.mutator(users_entity)
.updateOne(user.id, {
strategy_value: await strategy.hash(password as string),
});
togglePw(false);
if (await app.module.auth.changePassword(user.id, password)) {
$log.success(`Updated user: ${c.cyan(user.email)}`);
} catch (e) {
} else {
$log.error("Error updating user");
$console.error(e);
}
}