Logo Model Context Protocol TypeScript Implementation

Model Context Protocol TypeScript Implementation

Cộng đồng
hungryrobot1

A metacircular lambda calculus evaluator with MCP surface and runtime tool composition.

Nhà phát hànhhungryrobot1
Kho mãMCP-PIF
Ngôn ngữHaskell
Bản fork
17
Sao
57
Công cụ có sẵn
0
Phương thức truyền tảistdio
Danh mục
Liên kết
  • Kết nối công cụ với quy trình làm việc AI

    Model Context Protocol TypeScript Implementation cung cấp các khả năng MCP mà ứng dụng khách và tác nhân AI tương thích có thể sử dụng.

  • 0 công cụ có sẵn

    Xem các thao tác có thể gọi bên dưới, kèm tên và mô tả nếu máy chủ cung cấp.

  • Cấu hình sẵn để sao chép

    Dùng các đoạn cấu hình cài đặt mẫu để thiết lập máy chủ này trong ứng dụng khách MCP bạn muốn dùng.

  • Chỉ số quan tâm đến mã nguồn mở

    57 sao và 17 bản fork từ kho mã được liên kết.

MCP-PIF

A JSON-native lambda calculus runtime with metacircular evaluation, designed as an MCP (Model Context Protocol) server. Enables language models to evolve tools dynamically through metaprogramming.

Quick Start

bash
# Build the project
cabal build

# Enable debug mode for detailed evaluation tracing
MCP_DEBUG=1 cabal run mcp-pif
# Debug output (to stderr) shows:
# - Each evaluation step
# - Environment keys at each step
# - Closure creation and application
# - Tool code lookups

Basic Example

json
// Create a tool
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "evolve",
    "arguments": {
      "name": "square",
      "description": "Squares a number",
      "code": {"lam": "x", "body": {"mul": [{"var": "x"}, {"var": "x"}]}}
    }
  }
}

// Use the tool
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "run",
    "arguments": {
      "tool": "square",
      "input": 7
    }
  }
}
// Returns: 49

// Get help
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "help",
    "arguments": {"category": "lists"}
  }
}
// Returns: Documentation for list primitives

Core Concepts

The Problem

For pure computation language models need computational tools, but existing systems either provide fixed APIs or unrestricted code execution. In the context of metaprogramming and self-modification, neither is ideal. This program presents a middle ground structure for safe, inspectable, evolvable computation.

In the ideal sense, MCP-PIF can be thought of as a generic, metamorphic computer interface where the language model drops-in as an executive function. Here, we provide a simple vocabulary of lambda calculus primitives for access to pure computing only.

The Solution

MCP-PIF provides a lambda calculus with three metaprogramming primitives:

  • quote - Treat code as data (prevent evaluation)
  • eval - Execute quoted code dynamically
  • code_of - Introspect any tool's source code

This creates a metacircular system where tools can analyze and transform other tools while maintaining deterministic, fuel-bounded execution. It is homoiconic, but constrained.

It's worth noting quote and eval are not perfectly symmetric:

haskell
-- eval cleans the environment:
let cleanEnv = M.filterWithKey (\k _ -> not $ k `elem` ["__tool_name", "__self"]) env

This prevents eval'd code from inheriting the wrong tool context. When eval executes quoted code:

  • User variables ARE preserved (lexical scoping maintained)
  • System variables are cleaned (__tool_name and __self removed to prevent tool context confusion)
  • Tool codes remain available (for code_of introspection)
  • __eval_depth counter is added (max depth: 100, prevents infinite eval loops)

The Event Horizon

Because this framework is built in MCP it makes significant compromises in purity. Namely, updating the server code is not part of the metaprogramming loop. This means that the list of primitives, parsing strategies, and the evolution and evaluation processes themselves are not modifiable during runtime.

There are two kinds of tools:

  • MCP tools: For example creation via evolve (effectful, mutates registry)
  • Evolved tools: Tool execution via run (pure, functional)

Evolved tools can interact with each other but cannot themselves evolve new tools without the access to the protocol-level tool registry. This "simulacrum" implementation prevents unbounded self-modification by isolating the very invariants which enable the rich metaprogramming.

Language Reference

Core Primitives

CategoryPrimitiveJSON SyntaxDescription
LambdaVariable{"var": "x"}Variable reference
Function{"lam": "x", "body": ...}Lambda abstraction
Application{"app": {"func": ..., "arg": ...}}Function application
ArithmeticAdd{"add": [a, b]}Addition
Subtract{"sub": [a, b]}Subtraction
Multiply{"mul": [a, b]}Multiplication
Divide{"div": [a, b]}Division (errors on 0)
Modulo{"mod": [a, b]}Modulo (errors on 0)
ComparisonEqual{"eq": [a, b]}Equality test
Less Than{"lt": [a, b]}Less than
Less Than or Equal{"lte": [a, b]}Less than or equal
Greater Than{"gt": [a, b]}Greater than
Greater Than or Equal{"gte": [a, b]}Greater than or equal
LogicAnd{"and": [a, b]}Logical AND (short-circuit)
Or{"or": [a, b]}Logical OR (short-circuit)
Not{"not": a}Logical NOT
ControlIf{"if": {"cond": c, "then": t, "else": e}}Conditional
Continue{"continue": {"input": x}}Recursive step
ListsNil{"nil": true}Empty list
Cons{"cons": {"head": ..., "tail": ...}}List construction
Fold{"fold": [func, init, list]}Universal reducer (see GUIDE.md for pair parameter details)
PairsPair{"pair": [a, b]}Pair construction
First{"fst": pair}Get first element
Second{"snd": pair}Get second element
MetaQuote{"quote": term}Prevent evaluation
Eval{"eval": quoted}Execute quoted code
Code Of{"code_of": "tool_name"}Get tool source
Self{"self": true}Current closure reference (tools only)

Literals

  • Numbers: 42, -17, 3.14 → Integers (floats rounded)
  • Booleans: true, false
  • Strings: "hello", "world"
  • Arrays: [1, 2, 3] → Converted to cons lists
  • Null: null → Unit value

Input Normalization

The run tool automatically normalizes inputs to make CLI usage more ergonomic:

  • String numbers: "42" → 42
  • String booleans: "true" → true, "false" → false
  • JSON strings: "{\"x\": 5}" → Parsed as JSON object
  • Lists: JSON arrays are converted to cons lists

This allows flexible input formats while maintaining type safety during evaluation.

Quick Reference

For detailed patterns and examples, see the User Guide.

Key Concepts to Remember

  1. Tool References: Tools can be referenced as strings ("square"), inline lambdas, or via code_of
  2. Eval Scoping: User variables are preserved, system variables are cleaned, tool codes remain available
  3. Fold Signature: The fold function receives a single pair (accumulator, item), not two parameters
  4. Continuations: Only work with registered tools, each step is an MCP round-trip
  5. Self Reference: {"self": true} only works inside registered tools (created via evolve), not in inline lambdas passed to run
  6. Event Horizon: Tools cannot create other tools from within lambda calculus

Recursion Quick Reference

Need to recurse? Use this decision tree:

Can you structure it with an accumulator?
├─ Yes → Use `continue` (works for any depth)
│         Pattern: take pair [state, accumulator]
│         Base case: return accumulator
│         Recursive: compute new accumulator, continue with [new_state, new_acc]
│
└─ No, need result immediately?
   ├─ Small input (n < 20) → Use `self`
   └─ Large input → Redesign with accumulator or use fold

Examples:

  • Factorial → continue with accumulator pattern
  • Sum → continue with accumulator pattern
  • Fibonacci (small n) → self
  • Even/Odd mutual recursion → eval + code_of

Patterns & Examples

Recursive Factorial

Tools can use continuation-based recursion for step-by-step execution. Since continue pauses evaluation and returns control to the MCP layer, you must use an accumulator pattern where computation happens during recursion, not after:

json
{
  "name": "evolve",
  "arguments": {
    "name": "factorial",
    "description": "Computes factorial using continuation with accumulator",
    "code": {
      "lam": "n_acc",
      "body": {
        "if": {
          "cond": {"lte": [{"fst": {"var": "n_acc"}}, 1]},
          "then": {"snd": {"var": "n_acc"}},
          "else": {
            "continue": {
              "input": {
                "pair": [
                  {"sub": [{"fst": {"var": "n_acc"}}, 1]},
                  {"mul": [{"fst": {"var": "n_acc"}}, {"snd": {"var": "n_acc"}}]}
                ]
              }
            }
          }
        }
      }
    }
  }
}

Usage:

json
{
  "name": "run",
  "arguments": {
    "code": "factorial",
    "input": {"pair": [5, 1]}
  }
}

The program will return a structured response:

json
{
  "type": "continuation",
  "message": "Recursive step needed. Call run again with:",
  "tool": "factorial_acc",
  "next_input": {
    "pair": [4, 5]
  },
  "step": 1
}

This renders for the client as a Haskell representation:

haskell
Object (fromList [("message",String "Recursive step needed. Call run again with:"),("next_input",Object (fromList [("pair",Array [Number 4.0,Number 5.0])])),("step",Number 1.0),("tool",String "factorial_acc"),("type",String "continuation")])

Important: The tool takes a pair [n, accumulator] as input. Start with [5, 1] to compute 5!. Each continuation step multiplies the accumulator by the current n, then decrements n.

Why this pattern?

The continue primitive doesn't return a value you can compute with—it returns a continuation marker. All computation must happen before calling continue, stored in the accumulator. The pattern is:

  • Input: [n, acc] where acc holds the partial result
  • Base case: When n ≤ 1, return the accumulator
  • Recursive case: Compute new accumulator (n * acc), continue with [n-1, new_acc]

Alternative: Direct recursion with self (fuel-limited):

json
{
  "name": "evolve",
  "arguments": {
    "name": "factorial_self",
    "description": "Simple factorial using self (small n only)",
    "code": {
      "lam": "n",
      "body": {
        "if": {
          "cond": {"lte": [{"var": "n"}, 1]},
          "then": 1,
          "else": {
            "mul": [
              {"var": "n"},
              {"app": {"func": {"self": true}, "arg": {"sub": [{"var": "n"}, 1]}}}
            ]
          }
        }
      }
    }
  }
}

This works for small inputs but will hit the fuel limit (10,000 steps) around n=20.

Higher-Order Map via Fold

json
{
  "name": "map",
  "description": "Maps a function over a list",
  "code": {
    "lam": "f",
    "body": {
      "lam": "list",
      "body": {
        "fold": [
          {"lam": "acc_item", "body": {
            "cons": {
              "head": {"app": {"func": {"var": "f"}, "arg": {"snd": {"var": "acc_item"}}}},
              "tail": {"fst": {"var": "acc_item"}}
            }
          }},
          {"nil": true},
          {"var": "list"}
        ]
      }
    }
  }
}

Metaprogramming: Code Analysis

json
{
  "name": "count_operations",
  "description": "Counts arithmetic operations in a tool",
  "code": {
    "lam": "tool_name",
    "body": {
      "eval": {
        "quote": {
          "analyze": [{"code_of": {"var": "tool_name"}}]
        }
      }
    }
  }
}

The code_of primitive returns a tool's source as quoted data, enabling program analysis and transformation.

Architecture

Pipeline

JSON Input → Parser → Term → Evaluator → RuntimeValue → Encoder → JSON Output
         validation  syntax    execution     values     serialization

Modules

ModulePurpose
Main.hsEntry point, JSON-RPC loop
Server.hsMCP protocol, request routing
Core/Parser.hsJSON → Term validation
Core/Evaluator.hsTerm execution with fuel
Core/Encoder.hsRuntimeValue → JSON
Core/Types.hsCore type definitions
Core/Syntax.hsTerm ADT
Tools/Registry.hsTool storage

Safety Guarantees

  • Fuel-based termination: Every evaluation has finite steps (default: 10,000)
  • Pure evaluation: No I/O or effects in lambda calculus
  • Validated execution: Only structurally valid terms can run
  • Immutable registry: Tools can't modify each other during execution

MCP Integration

MCP-PIF implements the Model Context Protocol for tool discovery and execution:

System Tools

  • evolve - Create new tools (stores in registry)
  • run - Execute tools or inline lambda expressions
  • list - Show all registered tools
  • help - Display documentation for primitives and system tools

Protocol Flow

  1. Client sends JSON-RPC request to stdin
  2. Server parses and routes to appropriate handler
  3. For tool execution:
    • Parse input JSON → Term
    • Inject tool codes into environment
    • Evaluate with fuel limit
    • Encode result → JSON
  4. Response sent to stdout

Connecting an MCP Client

python
# Example using Python MCP SDK
import mcp

async with mcp.Client() as client:
    await client.connect(stdio_transport("cabal run mcp-pif"))

    # Create a tool
    await client.call_tool("evolve", {
        "name": "double",
        "description": "Doubles a number",
        "code": {"mul": [{"var": "x"}, 2]}
    })

    # Use it
    result = await client.call_tool("run", {
        "tool": "double",
        "input": 21
    })
    print(result)  # 42

Future Directions

MCP-PIF's current design maintains a clear boundary between pure computation and effectful operations. Several extensions have been considered that would expand these boundaries in interesting ways:

Analytic Functions

The current system is primarily synthetic - using primitives to compose new functions. A natural extension would be analytic capabilities:

  • Validation: Static analysis of term structure without evaluation
  • Normalization: Reducing terms to canonical forms
  • Equivalence Checking: Proving two terms compute the same function
  • Type Inference: Deriving types for lambda terms
  • Complexity Analysis: Estimating fuel requirement
  • Capabilities Anlysis: Deomposing tools by their primitives

These analytic functions would operate on code-as-data (quoted terms) and could enable powerful metaprogramming patterns. However, they require careful design to maintain the simplicity of the core calculus while providing meaningful guarantees.

Effectful Primitives

Another direction involves controlled introduction of effects:

System Interaction:

  • File I/O primitives (readFile, writeFile)
  • Process control (exec, env)
  • Network operations (fetch, serve)

Design Challenges:

  • How to maintain purity boundaries?
  • Should effects be monadic, algebraic, or continuation-based?
  • How to handle errors and resource management?
  • What security model for capability control?

One approach might be capability-based security: tools could declare required capabilities (file access, network, etc.) at creation time, with the MCP layer enforcing access control. This is consistent with the idea that evolved tools should bear proof of their own validity.

Self-Hosting & Bootstrapping

The ultimate metacircular goal: implementing MCP-PIF's evaluator in MCP-PIF itself. This would require:

  • Primitives for JSON manipulation
  • Pattern matching constructs
  • Efficient representation of environments
  • Fuel management at the meta-level

A self-hosted PIF could enable runtime evolution of the evaluation strategy itself - a truly reflective system.

Protocol Extensions

The MCP boundary could support additional protocol-level operations:

  • Tool versioning: Track tool evolution over time
  • Tool composition: Protocol-level combinators for tool fusion
  • Distributed registry: Share tools across MCP servers
  • Proof certificates: Attach correctness proofs to tools

These extensions maintain the event horizon principle while enriching the protocol layer's capabilities.

The key design principle for any extension: preserve the simplicity and predictability that makes PIF a reliable substrate for language model computation.

License

MIT

Cài đặt

TypingMind
{
  "mcpServers": {
    "mcp-pif": {
      "command": "node",
      "args": [
        "path/to/your/mcp-pif/build/index.js"
      ]
    }
  }
}

Dùng MCP Model Context Protocol TypeScript Implementation với nhiều mô hình AI

TypingMind kết nối các công cụ MCP ở cấp không gian làm việc, nên sau khi kết nối Model Context Protocol TypeScript Implementation, bạn có thể dùng với nhiều mô hình AI khác nhau trong TypingMind thay vì phải thiết lập riêng cho từng mô hình. MCP này chạy cục bộ thông qua trình kết nối MCP của TypingMind trên thiết bị của bạn.

Hướng dẫn thiết lập để dùng trình kết nối cục bộ

Dùng cách này khi máy chủ MCP cần truy cập các tệp, ứng dụng hoặc tài nguyên riêng tư trên máy tính của bạn.

1

Mở cài đặt MCP

Trong TypingMind, vào Cài đặt, Cài đặt nâng cao, rồi đến Giao thức ngữ cảnh mô hình và chọn Cài trình kết nối.

  1. Mở TypingMind trong trình duyệt của bạn.
  2. Nhấp vào biểu tượng Cài đặt.
  3. Vào Cài đặt nâng cao.
  4. Mở mục Giao thức ngữ cảnh mô hình.
  5. Nhấp vào Cài trình kết nối rồi chọn Thiết bị này.
Màn hình thiết lập trình kết nối MCP của TypingMind với tùy chọn Thiết bị này được chọn
2

Chạy lệnh trình kết nối

Chọn Thiết bị này, sao chép lệnh từ TypingMind và chạy trong Terminal. Giữ tiến trình chạy trong khi bạn dùng MCP.

  1. Sao chép lệnh thiết lập hiển thị trong TypingMind.
  2. Mở Terminal trên macOS hoặc Windows Terminal trên Windows.
  3. Dán và chạy lệnh.
  4. Xác nhận cài đặt gói nếu Terminal hỏi bạn có muốn tiếp tục không.
  5. Giữ tiến trình trong cửa sổ Terminal chạy trong khi dùng các công cụ MCP.
3

Thêm Model Context Protocol TypeScript Implementation làm máy chủ

Khi trạng thái trình kết nối là Sẵn sàng, nhấp vào Sửa máy chủ và dán cấu hình máy chủ MCP.

  1. Đợi đến khi trạng thái trình kết nối hiển thị Sẵn sàng.
  2. Nhấp vào Sửa máy chủ.
  3. Dán cấu hình máy chủ MCP của Model Context Protocol TypeScript Implementation.
  4. Lưu danh sách máy chủ.
  5. Làm mới nếu bạn muốn xác nhận trình kết nối vẫn sẵn sàng.
Cài đặt MCP của TypingMind hiển thị máy chủ đang hoạt động và nút Sửa máy chủ
{
  "mcpServers": {
    "mcp-pif": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-pif"
      ]
    }
  }
}
4

Dùng kết nối với nhiều mô hình

Lưu danh sách máy chủ, mở Plugin, bật các công cụ MCP của Model Context Protocol TypeScript Implementation, sau đó chọn bất kỳ mô hình AI nào được hỗ trợ trong TypingMind và dùng các công cụ trong cuộc trò chuyện hoặc gán chúng cho một tác nhân AI.

  1. Mở trang Plugin trong TypingMind.
  2. Bật các công cụ MCP của Model Context Protocol TypeScript Implementation.
  3. Bắt đầu cuộc trò chuyện và chọn mô hình AI bạn muốn dùng.
  4. Dùng các công cụ MCP trong cuộc trò chuyện hoặc gán chúng cho một tác nhân AI.
  5. Chuyển sang mô hình AI khác bất cứ khi nào cần mà không phải kết nối lại MCP.
Cuộc trò chuyện TypingMind dùng các công cụ MCP đã bật với một mô hình AI được chọn
Bạn có thể dùng Model Context Protocol TypeScript Implementation giúp tôi làm việc này không?
Model Context Protocol TypeScript Implementation
Được. Tôi đã đọc.
Đây là kết quả tôi tìm được bằng Model Context Protocol TypeScript Implementation.

Câu hỏi thường gặp

Máy chủ MCP Model Context Protocol TypeScript Implementation dùng để làm gì?

Model Context Protocol TypeScript Implementation là máy chủ MCP cho phép các ứng dụng AI tương thích kết nối với công cụ và ngữ cảnh bên ngoài. Trong TypingMind, bạn chỉ cần thêm máy chủ MCP này một lần để các công cụ của nó có sẵn trong không gian làm việc AI của bạn.

Bạn có thể dùng MCP Model Context Protocol TypeScript Implementation với nhiều mô hình AI trong TypingMind không?

Có. TypingMind kết nối các công cụ MCP ở cấp không gian làm việc, nên bạn có thể dùng Model Context Protocol TypeScript Implementation với nhiều mô hình AI như Claude, ChatGPT, Gemini hoặc các mô hình khác đã cấu hình trong TypingMind mà không cần thiết lập máy chủ MCP riêng cho từng mô hình.

Vì sao nên dùng MCP Model Context Protocol TypeScript Implementation với TypingMind?

TypingMind là một trong những giao diện trò chuyện với LLM tốt nhất vì tập hợp nhiều mô hình AI, prompt, plugin, tác nhân AI, khóa API và công cụ MCP vào một không gian làm việc. Khi kết nối Model Context Protocol TypeScript Implementation, bạn có thể dùng các công cụ MCP của dịch vụ này với những mô hình ưa thích, đồng thời giữ quy trình trò chuyện gọn gàng trong TypingMind.

Làm cách nào để kết nối MCP Model Context Protocol TypeScript Implementation với TypingMind?

Model Context Protocol TypeScript Implementation hoạt động thông qua trình kết nối MCP cục bộ của TypingMind. Đây là lựa chọn phù hợp nhất khi máy chủ MCP cần truy cập tệp cục bộ, ứng dụng máy tính, công cụ dòng lệnh hoặc tài nguyên riêng trên máy tính của bạn.

MCP Model Context Protocol TypeScript Implementation cung cấp những công cụ nào trong TypingMind?

Model Context Protocol TypeScript Implementation cung cấp các chức năng MCP mà bạn có thể bật từ trang Plugin của TypingMind để dùng trong trò chuyện hoặc gán cho tác nhân AI.

Bạn có cần chia sẻ khóa API với TypingMind để dùng MCP Model Context Protocol TypeScript Implementation không?

Không. TypingMind ưu tiên xử lý cục bộ và cho phép bạn kiểm soát nhà cung cấp mô hình, khóa API, prompt và cấu hình MCP. Nếu Model Context Protocol TypeScript Implementation yêu cầu xác thực, hãy thêm các header, cài đặt OAuth hoặc cấu hình cục bộ cần thiết cho máy chủ MCP đó khi tạo kết nối.

Máy chủ MCP liên quan

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇