Skip to content

Commit 318e664

Browse files
yigitkonurclaude
andcommitted
Add educational SAMPLE_TOOL_NAME environment variable feature
Implements optional dynamic tool registration via environment variable for educational purposes. When SAMPLE_TOOL_NAME is set, a simple echo tool is registered with that name, demonstrating runtime configuration patterns common in MCP servers. Changes: - Add conditional tool registration in registerCoreTools() that checks for SAMPLE_TOOL_NAME env var - Tool takes a 'value' string parameter and returns 'test string print: {value}' - Document the feature in README.md as an educational example of runtime configuration - Minimal implementation with zero impact when env var is not set 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent fb282ca commit 318e664

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ The server's behavior can be modified with command-line flags.
221221
| `--debug` | Enables verbose debug logging to `stderr`. |
222222
| `--help` | Shows the help message and exits. |
223223

224+
### Environment Variables
225+
226+
| Variable | Description | Default |
227+
| :----------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------ |
228+
| `SAMPLE_TOOL_NAME` | **(Educational)** Demonstrates dynamic tool registration via environment variables. When set, adds a simple echo tool with the specified name that takes a `value` parameter and returns `test string print: {value}`. This pattern shows how MCP servers can be configured at runtime. | None |
229+
224230
### Production Readiness Checklist
225231

226232
- [x] **Process Isolation:** The OS provides a natural security sandbox.

src/server.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,34 @@ function performBasicCalculation(op: string, a: number, b: number): number {
223223
* @param server - The MCP server instance to register tools with
224224
*/
225225
function registerCoreTools(server: McpServer): void {
226+
// Check for optional SAMPLE_TOOL_NAME environment variable
227+
const sampleToolName = process.env.SAMPLE_TOOL_NAME;
228+
if (sampleToolName) {
229+
// Register the sample tool with the name from environment variable
230+
server.registerTool(
231+
sampleToolName,
232+
{
233+
title: sampleToolName,
234+
description: 'Test tool for educational purposes',
235+
inputSchema: {
236+
value: z.string().describe('String value to process'),
237+
},
238+
},
239+
async ({ value }) => {
240+
log.info(`Executing ${sampleToolName} with value: ${value}`);
241+
const result = `test string print: ${value}`;
242+
return {
243+
content: [
244+
{
245+
type: 'text',
246+
text: result,
247+
},
248+
],
249+
};
250+
},
251+
);
252+
}
253+
226254
// --- TOOL: calculate ---
227255
// The most fundamental tool. Demonstrates basic SDK usage.
228256

0 commit comments

Comments
 (0)