aem-mcp-server
Version:
AEM MCP server. Chat with your AEM instance for content, component, and asset operations.
2 lines (1 loc) • 2.92 kB
JavaScript
;import{CallToolRequestSchema as y,ListToolsRequestSchema as E,ListResourcesRequestSchema as q,ReadResourceRequestSchema as O,InitializeRequestSchema as S,LATEST_PROTOCOL_VERSION as v,SUPPORTED_PROTOCOL_VERSIONS as U}from"@modelcontextprotocol/sdk/types.js";import{Server as T}from"@modelcontextprotocol/sdk/server/index.js";import{tools as p,injectInstanceParam as I,toolAnnotations as d}from"./mcp.tools.js";import{getResourceDefinitions as x,readResource as P}from"./mcp.resources.js";import{InstanceRegistry as A}from"./mcp.instances.js";import{LOGGER as a}from"../utils/logger.js";export const createMCPServer=g=>{const i=new A(g),c=i.getInstanceNames(),l=c.length>1,m={name:"aem-mcp-server",version:"1.3.9"},R={capabilities:{resources:{},tools:{}},instructions:l?`AEM MCP server with ${c.length} instances: ${c.join(", ")}. Use the "instance" parameter to target a specific instance (default: "${i.getDefaultName()}").`:"This is an AEM MCP server that provides tools for managing AEM components and content."},s=new T(m,R);return s.setRequestHandler(S,t=>{const e=t.params.protocolVersion,o=U.includes(e)?e:v;return a.log("1. Received InitializeRequest",t,"response:",{protocolVersion:o}),{protocolVersion:o,...R,serverInfo:m}}),s.setRequestHandler(E,async()=>{const t=l?I(p,c,i.getDefaultName()):p;return a.log("2. Received ListToolsRequest",t),{tools:t.map(e=>({...e,...d[e.name]&&{annotations:d[e.name]}}))}}),s.setRequestHandler(q,async()=>{const t=c.flatMap(e=>x(e));return a.log("Received ListResourcesRequest",t),{resources:t}}),s.setRequestHandler(O,async t=>{const{uri:e}=t.params;a.log("Received ReadResourceRequest",e);const r=e.match(/^aem:\/\/([^/]+)\//)?.[1],h=i.getHandler(r).aemConnector;return{contents:[await P(e,h)]}}),s.setRequestHandler(y,async t=>{const{name:e,arguments:o}=t.params;if(a.log("3. Received CallToolRequestSchema",t.params),!o)return{content:[{type:"text",text:"Error: No arguments provided"}],isError:!0};try{const{instance:r,...u}=o,n=await i.getHandler(r).handleRequest(e,u);if(n&&typeof n=="object"&&"error"in n&&n.error?.code==="OAUTH_REQUIRED"){const f={error:"OAuth authentication required",message:n.error.message,authUrl:n.error.authUrl,redirectUri:n.error.redirectUri,instructions:"Please open the authUrl in your browser to authorize the application. After authorization, you can retry this operation."};return{content:[{type:"text",text:JSON.stringify(f,null,2)}],isError:!0}}return{content:[{type:"text",text:JSON.stringify(n,null,2)}]}}catch(r){if(a.error("ERROR CallToolRequestSchema",r.message),r.code==="OAUTH_REQUIRED"&&r.authUrl){const u={error:"OAuth authentication required",message:r.message,authUrl:r.authUrl,instructions:"Please open the authUrl in your browser to authorize the application."};return{content:[{type:"text",text:JSON.stringify(u,null,2)}],isError:!0}}return{content:[{type:"text",text:`Error: ${r.message}`}],isError:!0}}}),s};