npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

function-calling

v1.5.0

Published

The easiest way to perform your chat completion to LLM with function calling.

Downloads

34

Readme

npm i function-calling

🤩 Key Features

  • 🔧 Automatically invoke tools.
  • 🎨 Nicely MCP Support.
  • 🔧→✉️→🔧→✉️ Continuously invoke tools in a loop until all tool call is satisfied.
  • 🧐 Dynamically build tools so we can adjust tools for each call. (Such as modify parameters)
  • 💰💰 Automatically summarize usages of internal chat completions.
  • 💦 Streaming callbacks.

😎 Getting Started

There's a example presents a situation that involves continuous function callings.

The user requested LLM to automatically adjust AC temperature🧊🔥 based on recently weather info.

Quick Look

import {
    functionalChatCompletion,
} from 'function-calling'
import OpenAI from 'openai'
async function chatWithGPT() {
    const openai = new OpenAI({
        baseURL: 'xxx',
        apiKey: 'xxx',
    })
    const { usage, messages } =
        await functionalChatCompletion(openai, {
            mcpClients: [yourMCPClientA,yourMCPClientB],// optional, all tools inside it will be automatically executed.
            tools: [ // optional, your custom javascript tools.
                getWeather, adjustAirConditioner
            ]
            stream: true,
            model: 'gpt-4.1',
            onTextDelta: (text) => console.log(text),
            messages: [
                {
                    role: 'user',
                    content:
                        'Hello, please query weather and adjust my AC!',
                },
            ],
        })
}

The returning message will presents its internal calling procedure.

const messages = [
    {
        role: 'assistant',
        content: "I'll try my best to query weather info! ",
        tool_call: [
            {
                id: 'abc',
                type: 'function',
                function: {
                    name: 'GetWeather',
                    arguments: "{ 'futureDays':7 }",
                },
            },
        ],
    },
    {
        role: 'tool',
        content: 'The weather info of 7 is XXXXX',
        tool_call_id: 'abc',
    },
    {
        role: 'assistant',
        content:
            "The temperature will drop in future 7 days, I'm increasing your AC temperature.",
        tool_call: [
            {
                id: 'def',
                type: 'function',
                function: {
                    name: 'AdjustAirConditioner',
                    arguments: '{ temperature: 25 }',
                },
            },
        ],
    },
    {
        role: 'tool',
        content: 'Adjusted',
        tool_call_id: 'def',
    },
    {
        role: 'assistant',
        content:
            'All done, please feel free to enjoy all my services. What can I do for you next?',
    },
]

Custom Script Tools

import {
    buildTool,
} from '@/index'
const getWeather = buildTool({
    name: 'GetWeather',
    description:
        'Get the weather info of where the user is',
    schema: z.object({
        futureDays: z.number().positive(),
    }),
    func: async ({ futureDays }) => {
        return `The weather info of ${futureDays}`
    },
})
const adjustAirConditioner = buildTool({
    name: 'AdjustAirConditioner',
    description:"Adjust user's Air conditioner"
    schema: z.object({
        temperature: z.number().positive().min(18).max(27),
    }),
    func: async (args) => {
        await externalLibrary.adjustACTemp(args.temperature)
    },
})

MCP Tools

import { getToolsOfMCPClient } from 'function-calling'
// Convert all mcp sdk format tools to
// function-calling's format. It will eventually
// convert to OpenAI format during executing.
const yourMCPTools = await getToolsOfMCPClient(yourClient)

// But you don't have to care about any further operation.
// Just passing it.
await functionalChatCompletion(openai, {
    tools: [
        getWeather, adjustAirConditioner, ...yourMCPTools
    ]
    stream: true,
    model: 'gpt-4.1',
    onTextDelta: (text) => console.log(text),
    messages: [
        {
            role: 'user',
            content:
                'Hello, please query weather and adjust my AC!',
        },
    ],
})

Which Way?

Q: which way should I choose to pass mcp tools to LLM?

A: It depends on your requirements.

First Way
const yourMCPTools = await getToolsOfMCPClient(yourClient)

then pass it

{
    //...
    tools: [
        getWeather,
        adjustAirConditioner,
        ...yourMCPTools,
    ]
    //...
}

Second Way

Our library will call getToolsOfMCPClient inside function.

{
    //...
    clients: [yourClient]
    tools: [
        getWeather,
        adjustAirConditioner,
        ...yourMCPTools,
    ]
    //...
}

Contribution

Build

We recommend to use pnpm as your package manager.

pnpm build

Test

We are using vitest for unit tests.

pnpm test

Contribution

Once you have developed codes, please raise PR to dev branch.

All pull requests are welcomed.