|
349 | 349 | (s/def ::parse-nrepl-port boolean?) |
350 | 350 | (s/def ::nrepl-args (s/keys :req-un [] |
351 | 351 | :opt-un [::port ::host ::config-file ::project-dir ::nrepl-env-type |
352 | | - ::start-nrepl-cmd ::parse-nrepl-port])) |
| 352 | + ::start-nrepl-cmd ::parse-nrepl-port])) |
353 | 353 |
|
354 | 354 | (def nrepl-client-atom (atom nil)) |
355 | 355 |
|
|
374 | 374 | :spec-data (s/explain-data ::nrepl-args opts)}))) |
375 | 375 | opts))) |
376 | 376 |
|
| 377 | +(defn ensure-port |
| 378 | + "Ensures the args map contains a :port key. |
| 379 | + Throws an exception with helpful context if port is missing. |
| 380 | + |
| 381 | + Args: |
| 382 | + - args: Map that should contain :port |
| 383 | + |
| 384 | + Returns: args unchanged if :port exists |
| 385 | + |
| 386 | + Throws: ExceptionInfo if :port is missing" |
| 387 | + [args] |
| 388 | + (if (:port args) |
| 389 | + args |
| 390 | + (throw |
| 391 | + (ex-info |
| 392 | + "No nREPL port available - either provide :port or configure auto-start" |
| 393 | + {:provided-args args})))) |
| 394 | + |
| 395 | +(defn register-components |
| 396 | + "Registers tools, prompts, and resources with the MCP server, applying config-based filtering. |
| 397 | + |
| 398 | + Args: |
| 399 | + - mcp-server: The MCP server instance to add components to |
| 400 | + - nrepl-client-map: The nREPL client map containing config |
| 401 | + - components: Map with :tools, :prompts, and :resources sequences |
| 402 | + |
| 403 | + Side effects: |
| 404 | + - Adds enabled components to the MCP server |
| 405 | + - Logs debug messages for enabled components |
| 406 | + |
| 407 | + Returns: nil" |
| 408 | + [mcp-server nrepl-client-map {:keys [tools prompts resources]}] |
| 409 | + ;; Register tools with filtering |
| 410 | + (doseq [tool tools] |
| 411 | + (when (config/tool-id-enabled? nrepl-client-map (:id tool)) |
| 412 | + (log/debug "Enabling tool:" (:id tool)) |
| 413 | + (add-tool mcp-server tool))) |
| 414 | + |
| 415 | + ;; Register resources with filtering |
| 416 | + (doseq [resource resources] |
| 417 | + (when (config/resource-name-enabled? nrepl-client-map (:name resource)) |
| 418 | + (log/debug "Enabling resource:" (:name resource)) |
| 419 | + (add-resource mcp-server resource))) |
| 420 | + |
| 421 | + ;; Register prompts with filtering |
| 422 | + (doseq [prompt prompts] |
| 423 | + (when (config/prompt-name-enabled? nrepl-client-map (:name prompt)) |
| 424 | + (log/debug "Enabling prompt:" (:name prompt)) |
| 425 | + (add-prompt mcp-server prompt))) |
| 426 | + nil) |
| 427 | + |
| 428 | +(defn build-components |
| 429 | + "Builds tools, prompts, and resources using the provided factory functions. |
| 430 | + |
| 431 | + Args: |
| 432 | + - nrepl-client-atom: Atom containing the nREPL client |
| 433 | + - working-dir: Working directory path |
| 434 | + - component-factories: Map with factory functions |
| 435 | + - :make-tools-fn - (fn [nrepl-client-atom working-dir] ...) returns seq of tools |
| 436 | + - :make-prompts-fn - (fn [nrepl-client-atom working-dir] ...) returns seq of prompts |
| 437 | + - :make-resources-fn - (fn [nrepl-client-atom working-dir] ...) returns seq of resources |
| 438 | + |
| 439 | + Returns: Map with :tools, :prompts, and :resources sequences" |
| 440 | + [nrepl-client-atom working-dir {:keys [make-tools-fn |
| 441 | + make-prompts-fn |
| 442 | + make-resources-fn] |
| 443 | + :as component-factories}] |
| 444 | + {:tools (when make-tools-fn |
| 445 | + (doall (make-tools-fn nrepl-client-atom working-dir))) |
| 446 | + :prompts (when make-prompts-fn |
| 447 | + (doall (make-prompts-fn nrepl-client-atom working-dir))) |
| 448 | + :resources (when make-resources-fn |
| 449 | + (doall (make-resources-fn nrepl-client-atom working-dir)))}) |
| 450 | + |
| 451 | +(defn setup-mcp-server |
| 452 | + "Sets up an MCP server by building components, creating the server, and registering components. |
| 453 | + |
| 454 | + This function encapsulates the common pattern used by both stdio and SSE transports: |
| 455 | + 1. Build components using factory functions |
| 456 | + 2. Create the MCP server (transport-specific) |
| 457 | + 3. Register components with filtering |
| 458 | + |
| 459 | + Args: |
| 460 | + - nrepl-client-atom: Atom containing the nREPL client map |
| 461 | + - working-dir: Working directory path |
| 462 | + - component-factories: Map with factory functions (:make-tools-fn, :make-prompts-fn, :make-resources-fn) |
| 463 | + - server-thunk: Zero-argument function that creates and returns a map with :mcp-server |
| 464 | + |
| 465 | + The server-thunk is called AFTER components are built but BEFORE they are registered, |
| 466 | + ensuring components are ready for immediate registration once the server starts. |
| 467 | + |
| 468 | + Returns: The result map from server-thunk (containing at least :mcp-server)" |
| 469 | + [nrepl-client-atom working-dir component-factories server-thunk] |
| 470 | + ;; Build components first to minimize latency |
| 471 | + (let [components (build-components nrepl-client-atom working-dir component-factories) |
| 472 | + ;; Create server after components are ready |
| 473 | + server-result (server-thunk) |
| 474 | + mcp-server (:mcp-server server-result)] |
| 475 | + ;; Register components with filtering |
| 476 | + (register-components mcp-server @nrepl-client-atom components) |
| 477 | + server-result)) |
| 478 | + |
377 | 479 | (defn build-and-start-mcp-server-impl |
378 | 480 | "Internal implementation of MCP server startup. |
379 | 481 | |
|
392 | 494 | - :host (optional) - nREPL server host (defaults to localhost) |
393 | 495 | - :project-dir (optional) - Root directory for the project (must exist) |
394 | 496 | |
395 | | - - config: Map with factory functions |
| 497 | + - component-factories: Map with factory functions |
396 | 498 | - :make-tools-fn - (fn [nrepl-client-atom working-dir] ...) returns seq of tools |
397 | 499 | - :make-prompts-fn - (fn [nrepl-client-atom working-dir] ...) returns seq of prompts |
398 | 500 | - :make-resources-fn - (fn [nrepl-client-atom working-dir] ...) returns seq of resources |
|
404 | 506 | - Starts the MCP server on stdio |
405 | 507 | |
406 | 508 | Returns: nil" |
407 | | - [nrepl-args {:keys [make-tools-fn |
408 | | - make-prompts-fn |
409 | | - make-resources-fn]}] |
| 509 | + [nrepl-args component-factories] |
410 | 510 | ;; the nrepl-args are a map with :port and optional :host |
411 | 511 | ;; Note: validation should be done by caller |
412 | 512 | (let [_ (assert (:port nrepl-args) "Port must be provided for build-and-start-mcp-server-impl") |
|
417 | 517 | (assoc nrepl-client-map :nrepl-process process) |
418 | 518 | nrepl-client-map) |
419 | 519 | _ (reset! nrepl-client-atom nrepl-client-with-process) |
420 | | - resources (when make-resources-fn |
421 | | - (doall (make-resources-fn nrepl-client-atom working-dir))) |
422 | | - tools (when make-tools-fn |
423 | | - (doall (make-tools-fn nrepl-client-atom working-dir))) |
424 | | - prompts (when make-prompts-fn |
425 | | - (doall (make-prompts-fn nrepl-client-atom working-dir))) |
426 | | - mcp (mcp-server)] |
427 | | - (doseq [tool tools] |
428 | | - (when (config/tool-id-enabled? nrepl-client-map (:id tool)) |
429 | | - (log/debug "Enabling tool:" (:id tool)) |
430 | | - (add-tool mcp tool))) |
431 | | - (doseq [resource resources] |
432 | | - (when (config/resource-name-enabled? nrepl-client-map (:name resource)) |
433 | | - (log/debug "Enabling resource:" (:name resource)) |
434 | | - (add-resource mcp resource))) |
435 | | - (doseq [prompt prompts] |
436 | | - (when (config/prompt-name-enabled? nrepl-client-map (:name prompt)) |
437 | | - (log/debug "Enabling prompt:" (:name prompt)) |
438 | | - (add-prompt mcp prompt))) |
| 520 | + ;; Setup MCP server with stdio transport |
| 521 | + server-result (setup-mcp-server nrepl-client-atom |
| 522 | + working-dir |
| 523 | + component-factories |
| 524 | + ;; stdio server creation thunk returns map |
| 525 | + (fn [] {:mcp-server (mcp-server)})) |
| 526 | + mcp (:mcp-server server-result)] |
439 | 527 | (swap! nrepl-client-atom assoc :mcp-server mcp) |
440 | 528 | nil)) |
441 | 529 |
|
|
460 | 548 | - :start-nrepl-cmd (optional) - Command to start nREPL server |
461 | 549 | - :parse-nrepl-port (optional) - Parse port from command output (default true) |
462 | 550 | |
463 | | - - config: Map with factory functions (same as |
464 | | - build-and-start-mcp-server-impl) |
| 551 | + - component-factories: Map with factory functions |
| 552 | + - :make-tools-fn - (fn [nrepl-client-atom working-dir] ...) returns seq of tools |
| 553 | + - :make-prompts-fn - (fn [nrepl-client-atom working-dir] ...) returns seq of prompts |
| 554 | + - :make-resources-fn - (fn [nrepl-client-atom working-dir] ...) returns seq of resources |
465 | 555 | |
466 | 556 | Auto-start conditions (must satisfy ONE): |
467 | 557 | 1. Both :start-nrepl-cmd AND :project-dir provided in nrepl-args |
468 | 558 | 2. Current directory contains .clojure-mcp/config.edn with :start-nrepl-cmd |
469 | 559 | |
470 | 560 | Returns: nil" |
471 | | - [nrepl-args config] |
472 | | - (let [validated-args (validate-options nrepl-args) |
473 | | - args-with-port (nrepl-launcher/maybe-start-nrepl-process |
474 | | - validated-args)] |
475 | | - ;; Ensure we have a port after auto-start or validation |
476 | | - (when-not (:port args-with-port) |
477 | | - (throw |
478 | | - (ex-info |
479 | | - "No nREPL port available - either provide :port or configure auto-start" |
480 | | - {:provided-args nrepl-args}))) |
481 | | - (build-and-start-mcp-server-impl args-with-port config))) |
| 561 | + [nrepl-args component-factories] |
| 562 | + (-> nrepl-args |
| 563 | + validate-options |
| 564 | + nrepl-launcher/maybe-start-nrepl-process |
| 565 | + ensure-port |
| 566 | + (build-and-start-mcp-server-impl component-factories))) |
0 commit comments