File tree Expand file tree Collapse file tree 2 files changed +36
-3
lines changed
Expand file tree Collapse file tree 2 files changed +36
-3
lines changed Original file line number Diff line number Diff line change 11{
22 "mcpServers" : {
33 "docker" : {
4- "command" : " npx " ,
5- "args" : [" -y " , " dist/index.js" ]
4+ "command" : " node " ,
5+ "args" : [" /Users/akoskm/Projects/egghead/mcp-server-stdio/ dist/index.js" ]
66 }
77 }
88}
Original file line number Diff line number Diff line change 11import { McpServer , ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js" ;
22import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js" ;
33import { z } from "zod" ;
4-
4+ import { exec } from "child_process" ;
55// Create an MCP server
66const server = new McpServer ( {
77 name : "Demo" ,
@@ -16,6 +16,39 @@ server.tool("add",
1616 } )
1717) ;
1818
19+ interface ProcessInfo {
20+ command : string ;
21+ pid : string | null ;
22+ }
23+
24+ server . tool ( "which-app-on-port" , { port : z . number ( ) } , async ( { port } ) => {
25+ const result = await new Promise < ProcessInfo > ( ( resolve , reject ) => {
26+ exec ( `lsof -t -i tcp:${ port } ` , ( error , pidStdout ) => {
27+ if ( error ) {
28+ reject ( error ) ;
29+ return ;
30+ }
31+ const pid = pidStdout . trim ( ) ;
32+ exec ( `ps -p ${ pid } -o comm=` , ( error , stdout ) => {
33+ if ( error ) {
34+ reject ( error ) ;
35+ return ;
36+ }
37+ resolve ( { command : stdout . trim ( ) , pid } ) ;
38+ } ) ;
39+ } ) ;
40+ } ) ;
41+
42+ const response = {
43+ pid : result . pid ,
44+ command : result . command ,
45+ } ;
46+
47+ return {
48+ content : [ { type : "text" , text : JSON . stringify ( response ) } ]
49+ } ;
50+ } ) ;
51+
1952// Add a dynamic greeting resource
2053server . resource (
2154 "greeting" ,
You can’t perform that action at this time.
0 commit comments