# Directory Structure
```
├── LICENSE
├── package.json
├── readme.md
├── src
│ └── index.ts
└── tsconfig.json
```
# Files
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
```markdown
# Jenkins Server MCP
A Model Context Protocol (MCP) server that provides tools for interacting with Jenkins CI/CD servers. This server enables AI assistants to check build statuses, trigger builds, and retrieve build logs through a standardized interface.
## Installation
1. Clone this repository:
```bash
git clone https://github.com/hekmon8/jenkins-server-mcp.git
cd jenkins-server-mcp
```
2. Install dependencies:
```bash
npm install
```
3. Build the project:
```bash
npm run build
```
## Configuration
The server requires the following environment variables:
- `JENKINS_URL`: The URL of your Jenkins server
- `JENKINS_USER`: Jenkins username for authentication
- `JENKINS_TOKEN`: Jenkins API token for authentication
Configure these in your MCP settings file:
### For Claude Desktop
MacOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
Windows: `%APPDATA%/Claude/claude_desktop_config.json`
```json
{
"mcpServers": {
"jenkins-server": {
"command": "node",
"args": ["/path/to/jenkins-server-mcp/build/index.js"],
"env": {
"JENKINS_URL": "https://your-jenkins-server.com",
"JENKINS_USER": "your-username",
"JENKINS_TOKEN": "your-api-token"
}
}
}
}
```
## Tools and Usage
### 1. Get Build Status
Get the status of a Jenkins build:
```typescript
// Example usage
const result = await mcpClient.useTool("jenkins-server", "get_build_status", {
jobPath: "view/xxx_debug",
buildNumber: "lastBuild" // Optional, defaults to lastBuild
});
```
Input Schema:
```json
{
"jobPath": "string", // Path to Jenkins job
"buildNumber": "string" // Optional, build number or "lastBuild"
}
```
### 2. Trigger Build
Trigger a new Jenkins build with parameters:
```typescript
// Example usage
const result = await mcpClient.useTool("jenkins-server", "trigger_build", {
jobPath: "view/xxx_debug",
parameters: {
BRANCH: "main",
BUILD_TYPE: "debug"
}
});
```
Input Schema:
```json
{
"jobPath": "string", // Path to Jenkins job
"parameters": {
// Build parameters as key-value pairs
}
}
```
### 3. Get Build Log
Retrieve the console output of a Jenkins build:
```typescript
// Example usage
const result = await mcpClient.useTool("jenkins-server", "get_build_log", {
jobPath: "view/xxx_debug",
buildNumber: "lastBuild"
});
```
Input Schema:
```json
{
"jobPath": "string", // Path to Jenkins job
"buildNumber": "string" // Build number or "lastBuild"
}
```
## Development
For development with auto-rebuild:
```bash
npm run watch
```
### Debugging
Since MCP servers communicate over stdio, you can use the MCP Inspector for debugging:
```bash
npm run inspector
```
This will provide a URL to access debugging tools in your browser.
## Thanks
Thanks AIMCP(https://www.aimcp.info).
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
```
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
```json
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
```
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
```json
{
"name": "jenkins-server",
"version": "0.1.0",
"description": "Jenkins Model Context Protocol server",
"private": true,
"type": "module",
"bin": {
"jenkins-server": "./build/index.js"
},
"files": [
"build"
],
"scripts": {
"build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\"",
"prepare": "npm run build",
"watch": "tsc --watch",
"inspector": "npx @modelcontextprotocol/inspector build/index.js"
},
"dependencies": {
"@modelcontextprotocol/sdk": "0.6.0",
"axios": "^1.7.9"
},
"devDependencies": {
"@types/node": "^20.11.24",
"typescript": "^5.3.3"
}
}
```
--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------
```typescript
#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ErrorCode,
ListToolsRequestSchema,
McpError,
} from '@modelcontextprotocol/sdk/types.js';
import axios from 'axios';
const JENKINS_URL = process.env.JENKINS_URL || '';
const JENKINS_USER = process.env.JENKINS_USER || '';
const JENKINS_TOKEN = process.env.JENKINS_TOKEN || '';
interface BuildStatus {
building: boolean;
result: string | null;
timestamp: number;
duration: number;
url: string;
}
class JenkinsServer {
private server: Server;
private axiosInstance;
constructor() {
this.server = new Server(
{
name: 'jenkins-server',
version: '0.1.0',
},
{
capabilities: {
tools: {},
},
}
);
this.axiosInstance = axios.create({
baseURL: JENKINS_URL,
auth: {
username: JENKINS_USER,
password: JENKINS_TOKEN,
},
});
this.setupToolHandlers();
// Error handling
this.server.onerror = (error) => console.error('[MCP Error]', error);
process.on('SIGINT', async () => {
await this.server.close();
process.exit(0);
});
}
private setupToolHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'get_build_status',
description: 'Get the status of a Jenkins build',
inputSchema: {
type: 'object',
properties: {
jobPath: {
type: 'string',
description: 'Path to the Jenkins job (e.g., "view/xxx_debug")',
},
buildNumber: {
type: 'string',
description: 'Build number (use "lastBuild" for most recent)',
},
},
required: ['jobPath'],
},
},
{
name: 'trigger_build',
description: 'Trigger a new Jenkins build',
inputSchema: {
type: 'object',
properties: {
jobPath: {
type: 'string',
description: 'Path to the Jenkins job',
},
parameters: {
type: 'object',
description: 'Build parameters (optional)',
additionalProperties: true,
},
},
required: ['jobPath', 'parameters'],
},
},
{
name: 'get_build_log',
description: 'Get the console output of a Jenkins build',
inputSchema: {
type: 'object',
properties: {
jobPath: {
type: 'string',
description: 'Path to the Jenkins job',
},
buildNumber: {
type: 'string',
description: 'Build number (use "lastBuild" for most recent)',
},
},
required: ['jobPath', 'buildNumber'],
},
},
],
}));
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
switch (request.params.name) {
case 'get_build_status':
return await this.getBuildStatus(request.params.arguments);
case 'trigger_build':
return await this.triggerBuild(request.params.arguments);
case 'get_build_log':
return await this.getBuildLog(request.params.arguments);
default:
throw new McpError(
ErrorCode.MethodNotFound,
`Unknown tool: ${request.params.name}`
);
}
} catch (error) {
if (error instanceof McpError) {
throw error;
}
if (axios.isAxiosError(error)) {
throw new McpError(
ErrorCode.InternalError,
`Jenkins API error: ${error.response?.data?.message || error.message}`
);
}
throw new McpError(ErrorCode.InternalError, 'Unknown error occurred');
}
});
}
private async getBuildStatus(args: any) {
const buildNumber = args.buildNumber || 'lastBuild';
const response = await this.axiosInstance.get<BuildStatus>(
`/${args.jobPath}/${buildNumber}/api/json`
);
return {
content: [
{
type: 'text',
text: JSON.stringify({
building: response.data.building,
result: response.data.result,
timestamp: response.data.timestamp,
duration: response.data.duration,
url: response.data.url,
}, null, 2),
},
],
};
}
private async triggerBuild(args: any) {
const params = new URLSearchParams();
if (args.parameters) {
Object.entries(args.parameters).forEach(([key, value]) => {
params.append(key, String(value));
});
}
await this.axiosInstance.post(
`/${args.jobPath}/buildWithParameters`,
params
);
return {
content: [
{
type: 'text',
text: 'Build triggered successfully',
},
],
};
}
private async getBuildLog(args: any) {
const response = await this.axiosInstance.get(
`/${args.jobPath}/${args.buildNumber}/consoleText`
);
return {
content: [
{
type: 'text',
text: response.data,
},
],
};
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error('Jenkins MCP server running on stdio');
}
}
const server = new JenkinsServer();
server.run().catch(console.error);
```