Skip to content
Merged

Alex #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ app.use('/mcp/v1', mcpRoutes);
// Mount GitHub OAuth routes at /auth/github
app.use('/auth/github', githubAuthRouter);

app.use('/jenkins', jenkinsRouter);
// // Mount GitHub OAuth routes at /auth/github
// app.use('/auth/github', githubAuthRouter);

// --- Global Error Handler ---
app.use((err, req, res, next) => {
console.error('Global Error:', err);
Expand All @@ -145,9 +149,7 @@ app.use((err, req, res, next) => {
});
});

app.use('/jenkins', jenkinsRouter);
// // Mount GitHub OAuth routes at /auth/github
// app.use('/auth/github', githubAuthRouter);


const port = process.env.PORT || 4000;
app.listen(port, () => console.log(`API on http://localhost:${port}`));
42 changes: 30 additions & 12 deletions server/src/agents/jenkins-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import { Agent, run, MCPServerStreamableHttp } from "@openai/agents";


export async function askJenkins(question) {
const user = process.env.JENKINS_USER;
const token = process.env.JENKINS_TOKEN;
// construct Basic Auth
const authToken = Buffer.from(
`${process.env.JENKINS_USER}:${process.env.JENKINS_TOKEN}`
).toString("base64");

if (!user || !token) {
throw new Error("JENKINS_USER or JENKINS_TOKEN is not set in the environment");
}

// 1. URL change /mcp-server/mcp
// 2. use requestInit.headers pass Authorization
console.log(authToken);
const jenkinsMcp = new MCPServerStreamableHttp({
name: "jenkins-mcp",
url: "https://jenkins.ilessai.com/mcp-server/mcp",
Expand All @@ -24,30 +30,42 @@ export async function askJenkins(question) {

await jenkinsMcp.connect();


try {
console.log("[askJenkins] connecting to Jenkins MCP…");
await jenkinsMcp.connect();
console.log("[askJenkins] connected. Listing tools…");

const tools = await jenkinsMcp.listTools();
console.log("[askJenkins] Jenkins MCP tools:", JSON.stringify(tools, null, 2));

const agent = new Agent({
name: "Jenkins Assistant",
instructions: `
You are an intelligent Jenkins assistant that can manage and query Jenkins jobs through the Model Context Protocol (MCP).
You have access to MCP tools provided by a Jenkins MCP server.
Use these tools whenever the user asks about:
- Job status, build history, build results
- Triggering builds
- Getting logs or console output
- Listing jobs or checking recent failures
Always use the appropriate MCP tool instead of making up an answer.
If the MCP response includes structured data (like JSON or status codes), summarize it clearly and naturally in plain English.
When the user asks something unrelated to Jenkins, politely clarify that you specialize in Jenkins automation and can help with jobs, builds, or logs.
If a tool call fails (for example, connection timeout or authentication error), provide a short diagnostic message like:
> “I wasn’t able to reach the Jenkins MCP server. Please verify your Jenkins URL or token.”
Keep answers short, factual, and professional. Use bullet points for lists or multiple jobs.
You have access to MCP tools provided by a Jenkins MCP server.

When asked things like "what jobs exist" or "list jobs", you MUST call the getJobs tool.
Do not guess job names or statuses — always call a tool.

If a tool call returns an error object like {"status":"FAILED","message":"..."},
briefly summarize that message to the user instead of hiding it.
`,
mcpServers: [jenkinsMcp],
});

console.log("[askJenkins] running agent with question:", question);
const result = await run(agent, question);

// Inspect the full result for debugging:
console.dir(result, { depth: 5 });

return result.finalOutput;
} catch (err) {
console.error("[askJenkins] ERROR:", err);
throw err;
} finally {
await jenkinsMcp.close();
console.log("[askJenkins] MCP connection closed.");
}
}