ADDING_TOOLS.md•4.08 kB
# Adding New Tools to the MCP Login Server
This guide explains how to add new tools to the MCP Login Server after the refactoring.
## Project Structure
```
src/
├── index.ts # Main server setup and entry point
├── tools.ts # Tool definitions and registration
└── index.js # Compiled JavaScript (auto-generated)
```
## Adding a New Tool
### 1. Define the Tool in `tools.ts`
Add a new tool registration function to `src/tools.ts`:
```typescript
/**
* Tool: your_new_tool
*
* Description of what your tool does.
*/
export function registerYourNewTool(server: McpServer): void {
server.tool(
"your_new_tool",
"Description of what your tool does",
{
// Define your tool's parameters using Zod schema
param1: z.string().describe("Description of parameter 1"),
param2: z.number().optional().describe("Optional parameter 2")
},
async ({ param1, param2 = defaultValue }) => {
try {
// Your tool logic here
return {
content: [
{
type: "text",
text: `Your tool response here`
}
]
};
} catch (error) {
throw new McpError(
ErrorCode.InternalError,
`Your tool failed: ${error instanceof Error ? error.message : String(error)}`
);
}
}
);
}
```
### 2. Register the Tool
Add your tool to the `registerAllTools` function:
```typescript
export function registerAllTools(server: McpServer): void {
registerPerformLoginTool(server);
registerGetLoginCredentialsTool(server);
registerTestConnectionTool(server);
registerYourNewTool(server); // Add this line
}
```
### 3. Add Tool Name to Available Tools List
Update the `getAvailableTools` function:
```typescript
export function getAvailableTools(): string[] {
return [
"perform_login",
"get_login_credentials",
"test_connection",
"your_new_tool" // Add this line
];
}
```
### 4. Build and Test
```bash
npm run build
npm start
```
## Example: Adding a Logout Tool
Here's a complete example of adding a logout tool:
```typescript
/**
* Tool: perform_logout
*
* Performs logout from the current session.
*/
export function registerPerformLogoutTool(server: McpServer): void {
server.tool(
"perform_logout",
"Performs logout from the current session",
{},
async () => {
try {
const logoutSteps = [
"Click on user menu",
"Select logout option",
"Confirm logout",
"Wait for redirect to login page"
];
return {
content: [
{
type: "text",
text: `Logout Tool Response:
✅ Tool: perform_logout
🚪 Action: Logout
📋 Logout Steps:
${logoutSteps.map((step, index) => `${index + 1}. ${step}`).join('\n')}
💡 Note: This tool provides logout instructions for browser automation.`
}
]
};
} catch (error) {
throw new McpError(
ErrorCode.InternalError,
`Logout tool failed: ${error instanceof Error ? error.message : String(error)}`
);
}
}
);
}
```
## Benefits of This Structure
1. **Separation of Concerns**: Tool definitions are separate from server setup
2. **Easy Extension**: Adding new tools requires minimal changes
3. **Maintainability**: Each tool is self-contained and documented
4. **Reusability**: Tools can be easily shared or moved between projects
5. **Testing**: Individual tools can be tested independently
## Available Tools
The server currently provides these tools:
- `perform_login`: Automated login to http://localhost
- `get_login_credentials`: Returns current login credentials
- `test_connection`: Tests connectivity to the target URL
## Configuration
Login credentials and server settings are centralized in `tools.ts` for easy modification:
```typescript
export const LOGIN_CREDENTIALS = {
username: "admin",
password: "AIWorkshopJuly!25",
targetUrl: "http://localhost"
};
```