All skills
medusajs avatar

/creating-internal-agents

@597f4a0 official
by Medusamedusajs/medusa-agent-skills225 stars
29

Use when building an internal admin-facing AI agent in a Medusa project. These agents are operated by merchants and store operators — not customers. Covers data models, module service, agent runtime (tools, system prompt, streamText), streaming API routes (NDJSON), and admin UI chat extensions. Load for any internal agent type: store operations assistant, product audit, cohort analysis, customer service tooling for support staff, etc. Do NOT use for customer-facing agents (storefront chatbots, buyer-side assistants).

  • 8 files
  • 46.7 KB
  • Updated 3 months ago
  • GitHub

Use this Skill: https://skilld.dev/gh/medusajs/medusa-agent-skills/creating-internal-agents

This session only. Nothing lands on disk.

referenceservice.md

≈780 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Service

The agent module is shared infrastructure — one service handles persistence and the AI client for every agent in the project. Each agent passes its own config (system prompt + tool descriptions) at call time, so different agents get different behaviour without separate modules.

Module Service

// src/modules/agent/service.ts
import { createAnthropic } from "@ai-sdk/anthropic"
import type { MedusaContainer } from "@medusajs/framework/types"
import { MedusaService } from "@medusajs/framework/utils"
import { medusaAgent } from "./agents"
import { AgentSession } from "./models/session"
import { AgentMessage } from "./models/message"

export type AgentModuleOptions = {
  apiKey: string
  model?: string  // defaults to "claude-sonnet-4-5"
}

export default class AgentModuleService extends MedusaService({
  AgentSession,
  AgentMessage,
}) {
  private model_: ReturnType<ReturnType<typeof createAnthropic>>

  constructor(_deps: unknown, options: AgentModuleOptions) {
    super(...arguments)
    const anthropic = createAnthropic({ apiKey: options.apiKey })
    this.model_ = anthropic(options.model ?? "claude-sonnet-4-5")
  }

  // config is passed per-call so each agent can use its own prompt and tools
  stream(messages: any[], container: MedusaContainer, config: any) {
    return medusaAgent({
      model: this.model_,
      messages,
      config,
      experimental_context: { medusa_container: container },
    })
  }
}

What MedusaService Gives You

Passing the model map to MedusaService({...}) auto-generates CRUD methods:

Pattern Example
create<Entity>s(data) createAgentSessions({ title, created_by_id })
list<Entity>s(filters, config) listAgentSessions({}, { order: { created_at: "DESC" }, take: 50 })
retrieve<Entity>(id) retrieveAgentSession(id)
update<Entity>s(id, data) updateAgentSessions(id, { title: "…" })
delete<Entity>s(id) deleteAgentSessions(id)

The method names are derived from the model class name (e.g., AgentSession → AgentSession).

All agents share these methods. Filter by agent_type when listing sessions to scope results to the calling agent.

Module Index File

// src/modules/agent/index.ts
import { Module } from "@medusajs/framework/utils"
import AgentModuleService from "./service"

export const AGENT_MODULE = "agentModule"

export default Module(AGENT_MODULE, {
  service: AgentModuleService,
})

The constant (AGENT_MODULE) is the key used to resolve the service from the container in API routes: req.scope.resolve(AGENT_MODULE)

Registering in medusa-config.ts

// medusa-config.ts
modules: [
  {
    resolve: "./src/modules/agent",
    options: {
      apiKey: process.env.ANTHROPIC_API_KEY,
      model: "claude-sonnet-4-5",  // optional, this is the default
    },
  },
]

CRITICAL: resolve must be the path to the module directory (containing index.ts). The options are forwarded to the service constructor.

Environment Variables

Add to .env:

ANTHROPIC_API_KEY=sk-ant-...

Source: SKILL.md on GitHub

No third-party reports yet.

Signed by skilld at 597f4a0. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 3 months ago

README badge

README badge for medusajs/medusa-agent-skills/creating-internal-agents