@@ -3,14 +3,20 @@ import { Agent, run, MCPServerStreamableHttp } from "@openai/agents";
33
44
55export async function askJenkins ( question ) {
6+ const user = process . env . JENKINS_USER ;
7+ const token = process . env . JENKINS_TOKEN ;
68 // construct Basic Auth
79 const authToken = Buffer . from (
810 `${ process . env . JENKINS_USER } :${ process . env . JENKINS_TOKEN } `
911 ) . toString ( "base64" ) ;
1012
13+ if ( ! user || ! token ) {
14+ throw new Error ( "JENKINS_USER or JENKINS_TOKEN is not set in the environment" ) ;
15+ }
1116
1217 // 1. URL change /mcp-server/mcp
1318 // 2. use requestInit.headers pass Authorization
19+ console . log ( authToken ) ;
1420 const jenkinsMcp = new MCPServerStreamableHttp ( {
1521 name : "jenkins-mcp" ,
1622 url : "https://jenkins.ilessai.com/mcp-server/mcp" ,
@@ -24,30 +30,42 @@ export async function askJenkins(question) {
2430
2531 await jenkinsMcp . connect ( ) ;
2632
33+
2734 try {
35+ console . log ( "[askJenkins] connecting to Jenkins MCP…" ) ;
36+ await jenkinsMcp . connect ( ) ;
37+ console . log ( "[askJenkins] connected. Listing tools…" ) ;
38+
39+ const tools = await jenkinsMcp . listTools ( ) ;
40+ console . log ( "[askJenkins] Jenkins MCP tools:" , JSON . stringify ( tools , null , 2 ) ) ;
41+
2842 const agent = new Agent ( {
2943 name : "Jenkins Assistant" ,
3044 instructions : `
3145 You are an intelligent Jenkins assistant that can manage and query Jenkins jobs through the Model Context Protocol (MCP).
32- You have access to MCP tools provided by a Jenkins MCP server.
33- Use these tools whenever the user asks about:
34- - Job status, build history, build results
35- - Triggering builds
36- - Getting logs or console output
37- - Listing jobs or checking recent failures
38- Always use the appropriate MCP tool instead of making up an answer.
39- If the MCP response includes structured data (like JSON or status codes), summarize it clearly and naturally in plain English.
40- When the user asks something unrelated to Jenkins, politely clarify that you specialize in Jenkins automation and can help with jobs, builds, or logs.
41- If a tool call fails (for example, connection timeout or authentication error), provide a short diagnostic message like:
42- > “I wasn’t able to reach the Jenkins MCP server. Please verify your Jenkins URL or token.”
43- Keep answers short, factual, and professional. Use bullet points for lists or multiple jobs.
46+ You have access to MCP tools provided by a Jenkins MCP server.
47+
48+ When asked things like "what jobs exist" or "list jobs", you MUST call the getJobs tool.
49+ Do not guess job names or statuses — always call a tool.
50+
51+ If a tool call returns an error object like {"status":"FAILED","message":"..."},
52+ briefly summarize that message to the user instead of hiding it.
4453 ` ,
4554 mcpServers : [ jenkinsMcp ] ,
4655 } ) ;
4756
57+ console . log ( "[askJenkins] running agent with question:" , question ) ;
4858 const result = await run ( agent , question ) ;
59+
60+ // Inspect the full result for debugging:
61+ console . dir ( result , { depth : 5 } ) ;
62+
4963 return result . finalOutput ;
64+ } catch ( err ) {
65+ console . error ( "[askJenkins] ERROR:" , err ) ;
66+ throw err ;
5067 } finally {
5168 await jenkinsMcp . close ( ) ;
69+ console . log ( "[askJenkins] MCP connection closed." ) ;
5270 }
5371}
0 commit comments